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 void wxComboBox::Init()
32 bool wxComboBox::Create(wxWindow
*parent
, wxWindowID id
,
33 const wxString
& value
,
36 const wxArrayString
& choices
,
38 const wxValidator
& validator
,
41 wxCArrayString
chs( choices
);
43 return Create( parent
, id
, value
, pos
, size
, chs
.GetCount(),
44 chs
.GetStrings(), style
, validator
, name
);
47 bool wxComboBox::Create(wxWindow
*parent
, wxWindowID id
,
48 const wxString
& value
,
51 int n
, const wxString choices
[],
53 const wxValidator
& validator
,
59 m_macIsUserPane
= false;
61 if ( !wxControl::Create( parent
, id
, pos
, size
, style
, validator
, name
) )
64 wxASSERT_MSG( !(style
& wxCB_SORT
),
65 "wxCB_SORT not currently supported by wxOSX/Cocoa");
67 m_peer
= wxWidgetImpl::CreateComboBox( this, parent
, id
, NULL
, pos
, size
, style
, GetExtraStyle() );
69 MacPostControlCreate( pos
, size
);
73 // Set up the initial value, by default the first item is selected.
79 // Needed because it is a wxControlWithItems
80 SetInitialSize( size
);
85 void wxComboBox::DelegateTextChanged( const wxString
& value
)
87 SetStringSelection( value
);
90 void wxComboBox::DelegateChoice( const wxString
& value
)
92 SetStringSelection( value
);
95 int wxComboBox::DoInsertItems(const wxArrayStringsAdapter
& items
,
97 void **clientData
, wxClientDataType type
)
99 const unsigned int numItems
= items
.GetCount();
100 for( unsigned int i
= 0; i
< numItems
; ++i
, ++pos
)
105 GetComboPeer()->InsertItem( idx
, items
[i
] );
107 if (idx
> m_datas
.GetCount())
108 m_datas
.SetCount(idx
);
109 m_datas
.Insert( NULL
, idx
);
110 AssignNewItemClientData(idx
, clientData
, i
, type
);
113 m_peer
->SetMaximum( GetCount() );
118 // ----------------------------------------------------------------------------
120 // ----------------------------------------------------------------------------
121 void wxComboBox::DoSetItemClientData(unsigned int n
, void* clientData
)
123 wxCHECK_RET( IsValid(n
), "invalid index" );
125 m_datas
[n
] = (char*)clientData
;
128 void * wxComboBox::DoGetItemClientData(unsigned int n
) const
130 wxCHECK_MSG( IsValid(n
), NULL
, "invalid index" );
132 return (void *)m_datas
[n
];
135 unsigned int wxComboBox::GetCount() const
137 return GetComboPeer()->GetNumberOfItems();
140 void wxComboBox::DoDeleteOneItem(unsigned int n
)
142 GetComboPeer()->RemoveItem(n
);
145 void wxComboBox::DoClear()
147 GetComboPeer()->Clear();
150 void wxComboBox::GetSelection(long *from
, long *to
) const
152 wxTextEntry::GetSelection(from
, to
);
155 int wxComboBox::GetSelection() const
157 return GetComboPeer()->GetSelectedItem();
160 void wxComboBox::SetSelection(int n
)
162 GetComboPeer()->SetSelectedItem(n
);
165 void wxComboBox::SetSelection(long from
, long to
)
167 wxTextEntry::SetSelection(from
, to
);
170 int wxComboBox::FindString(const wxString
& s
, bool bCase
) const
174 for (unsigned i
= 0; i
< GetCount(); i
++)
176 if (s
.IsSameAs(GetString(i
), false))
182 return GetComboPeer()->FindString(s
);
185 wxString
wxComboBox::GetString(unsigned int n
) const
187 wxCHECK_MSG( n
< GetCount(), wxString(), "Invalid combobox index" );
189 return GetComboPeer()->GetStringAtIndex(n
);
192 wxString
wxComboBox::GetStringSelection() const
194 const int sel
= GetSelection();
195 return sel
== wxNOT_FOUND
? wxString() : GetString(sel
);
198 void wxComboBox::SetString(unsigned int n
, const wxString
& s
)
202 SetValue(s
); // changing the item in the list won't update the display item
205 void wxComboBox::EnableTextChangedEvents(bool WXUNUSED(enable
))
207 // nothing to do here, events are never generated when we change the
208 // control value programmatically anyhow by Cocoa
211 bool wxComboBox::OSXHandleClicked( double WXUNUSED(timestampsec
) )
213 wxCommandEvent
event(wxEVT_COMMAND_COMBOBOX_SELECTED
, m_windowId
);
214 event
.SetInt(GetSelection());
215 event
.SetEventObject(this);
216 event
.SetString(GetStringSelection());
217 ProcessCommand(event
);
221 wxComboWidgetImpl
* wxComboBox::GetComboPeer() const
223 return dynamic_cast<wxComboWidgetImpl
*> (m_peer
);
226 #endif // wxUSE_COMBOBOX && wxOSX_USE_COCOA