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 IMPLEMENT_DYNAMIC_CLASS(wxComboBox
, wxControl
)
26 wxComboBox::~wxComboBox()
30 void wxComboBox::Init()
34 bool wxComboBox::Create(wxWindow
*parent
, wxWindowID id
,
35 const wxString
& value
,
38 const wxArrayString
& choices
,
40 const wxValidator
& validator
,
43 wxCArrayString
chs( choices
);
45 return Create( parent
, id
, value
, pos
, size
, chs
.GetCount(),
46 chs
.GetStrings(), style
, validator
, name
);
49 bool wxComboBox::Create(wxWindow
*parent
, wxWindowID id
,
50 const wxString
& value
,
53 int n
, const wxString choices
[],
55 const wxValidator
& validator
,
61 m_macIsUserPane
= false;
63 if ( !wxControl::Create( parent
, id
, pos
, size
, style
, validator
, name
) )
66 wxASSERT_MSG( !(style
& wxCB_SORT
),
67 "wxCB_SORT not currently supported by wxOSX/Cocoa");
69 m_peer
= wxWidgetImpl::CreateComboBox( this, parent
, id
, NULL
, pos
, size
, style
, GetExtraStyle() );
71 MacPostControlCreate( pos
, size
);
75 // Set up the initial value, by default the first item is selected.
81 // Needed because it is a wxControlWithItems
82 SetInitialSize( size
);
87 void wxComboBox::DelegateTextChanged( const wxString
& value
)
89 SetStringSelection( value
);
92 void wxComboBox::DelegateChoice( const wxString
& value
)
94 SetStringSelection( value
);
97 int wxComboBox::DoInsertItems(const wxArrayStringsAdapter
& items
,
99 void **clientData
, wxClientDataType type
)
101 const unsigned int numItems
= items
.GetCount();
102 for( unsigned int i
= 0; i
< numItems
; ++i
, ++pos
)
107 GetComboPeer()->InsertItem( idx
, items
[i
] );
109 if (idx
> m_datas
.GetCount())
110 m_datas
.SetCount(idx
);
111 m_datas
.Insert( NULL
, idx
);
112 AssignNewItemClientData(idx
, clientData
, i
, type
);
115 m_peer
->SetMaximum( GetCount() );
120 // ----------------------------------------------------------------------------
122 // ----------------------------------------------------------------------------
123 void wxComboBox::DoSetItemClientData(unsigned int n
, void* clientData
)
125 wxCHECK_RET( IsValid(n
), "invalid index" );
127 m_datas
[n
] = (char*)clientData
;
130 void * wxComboBox::DoGetItemClientData(unsigned int n
) const
132 wxCHECK_MSG( IsValid(n
), NULL
, "invalid index" );
134 return (void *)m_datas
[n
];
137 unsigned int wxComboBox::GetCount() const
139 return GetComboPeer()->GetNumberOfItems();
142 void wxComboBox::DoDeleteOneItem(unsigned int n
)
144 GetComboPeer()->RemoveItem(n
);
147 void wxComboBox::DoClear()
149 GetComboPeer()->Clear();
152 void wxComboBox::GetSelection(long *from
, long *to
) const
154 wxTextEntry::GetSelection(from
, to
);
157 int wxComboBox::GetSelection() const
159 return GetComboPeer()->GetSelectedItem();
162 void wxComboBox::SetSelection(int n
)
164 GetComboPeer()->SetSelectedItem(n
);
167 void wxComboBox::SetSelection(long from
, long to
)
169 wxTextEntry::SetSelection(from
, to
);
172 int wxComboBox::FindString(const wxString
& s
, bool bCase
) const
176 for (unsigned i
= 0; i
< GetCount(); i
++)
178 if (s
.IsSameAs(GetString(i
), false))
184 return GetComboPeer()->FindString(s
);
187 wxString
wxComboBox::GetString(unsigned int n
) const
189 wxCHECK_MSG( n
< GetCount(), wxString(), "Invalid combobox index" );
191 return GetComboPeer()->GetStringAtIndex(n
);
194 wxString
wxComboBox::GetStringSelection() const
196 const int sel
= GetSelection();
197 return sel
== wxNOT_FOUND
? wxString() : GetString(sel
);
200 void wxComboBox::SetString(unsigned int n
, const wxString
& s
)
204 SetValue(s
); // changing the item in the list won't update the display item
207 void wxComboBox::EnableTextChangedEvents(bool WXUNUSED(enable
))
209 // nothing to do here, events are never generated when we change the
210 // control value programmatically anyhow by Cocoa
213 bool wxComboBox::OSXHandleClicked( double WXUNUSED(timestampsec
) )
215 wxCommandEvent
event(wxEVT_COMMAND_COMBOBOX_SELECTED
, m_windowId
);
216 event
.SetInt(GetSelection());
217 event
.SetEventObject(this);
218 event
.SetString(GetStringSelection());
219 ProcessCommand(event
);
223 wxComboWidgetImpl
* wxComboBox::GetComboPeer() const
225 return dynamic_cast<wxComboWidgetImpl
*> (m_peer
);
228 #endif // wxUSE_COMBOBOX && wxOSX_USE_COCOA