1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/combobox_osx.cpp
3 // Purpose: wxComboBox class using HIView ComboBox
4 // Author: Stefan Csomor
7 // RCS-ID: $Id: combobox_osx.cpp 58318 2009-01-23 08:36:16Z RR $
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 return GetComboPeer()->GetStringAtIndex(n
);
192 wxString
wxComboBox::GetStringSelection() const
194 return GetString(GetSelection());
197 void wxComboBox::SetString(unsigned int n
, const wxString
& s
)
201 SetValue(s
); // changing the item in the list won't update the display item
204 void wxComboBox::EnableTextChangedEvents(bool WXUNUSED(enable
))
206 // nothing to do here, events are never generated when we change the
207 // control value programmatically anyhow by Cocoa
210 bool wxComboBox::OSXHandleClicked( double WXUNUSED(timestampsec
) )
212 wxCommandEvent
event(wxEVT_COMMAND_COMBOBOX_SELECTED
, m_windowId
);
213 event
.SetInt(GetSelection());
214 event
.SetEventObject(this);
215 event
.SetString(GetStringSelection());
216 ProcessCommand(event
);
220 wxComboWidgetImpl
* wxComboBox::GetComboPeer() const
222 return dynamic_cast<wxComboWidgetImpl
*> (m_peer
);
225 #endif // wxUSE_COMBOBOX && wxOSX_USE_COCOA