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
,
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 SetPeer(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 GetPeer()->SetMaximum( GetCount() );
118 // ----------------------------------------------------------------------------
120 // ----------------------------------------------------------------------------
121 void wxComboBox::DoSetItemClientData(unsigned int n
, void* clientData
)
123 m_datas
[n
] = (char*)clientData
;
126 void * wxComboBox::DoGetItemClientData(unsigned int n
) const
128 return (void *)m_datas
[n
];
131 unsigned int wxComboBox::GetCount() const
133 return GetComboPeer()->GetNumberOfItems();
136 void wxComboBox::DoDeleteOneItem(unsigned int n
)
138 GetComboPeer()->RemoveItem(n
);
141 void wxComboBox::DoClear()
143 GetComboPeer()->Clear();
146 void wxComboBox::GetSelection(long *from
, long *to
) const
148 wxTextEntry::GetSelection(from
, to
);
151 int wxComboBox::GetSelection() const
153 return GetComboPeer()->GetSelectedItem();
156 void wxComboBox::SetSelection(int n
)
158 GetComboPeer()->SetSelectedItem(n
);
161 void wxComboBox::SetSelection(long from
, long to
)
163 wxTextEntry::SetSelection(from
, to
);
166 int wxComboBox::FindString(const wxString
& s
, bool bCase
) const
170 for (unsigned i
= 0; i
< GetCount(); i
++)
172 if (s
.IsSameAs(GetString(i
), false))
178 return GetComboPeer()->FindString(s
);
181 wxString
wxComboBox::GetString(unsigned int n
) const
183 wxCHECK_MSG( n
< GetCount(), wxString(), "Invalid combobox index" );
185 return GetComboPeer()->GetStringAtIndex(n
);
188 wxString
wxComboBox::GetStringSelection() const
190 const int sel
= GetSelection();
191 return sel
== wxNOT_FOUND
? wxString() : GetString(sel
);
194 void wxComboBox::SetString(unsigned int n
, const wxString
& s
)
198 SetValue(s
); // changing the item in the list won't update the display item
201 void wxComboBox::EnableTextChangedEvents(bool WXUNUSED(enable
))
203 // nothing to do here, events are never generated when we change the
204 // control value programmatically anyhow by Cocoa
207 bool wxComboBox::OSXHandleClicked( double WXUNUSED(timestampsec
) )
209 wxCommandEvent
event(wxEVT_COMMAND_COMBOBOX_SELECTED
, m_windowId
);
210 event
.SetInt(GetSelection());
211 event
.SetEventObject(this);
212 event
.SetString(GetStringSelection());
213 ProcessCommand(event
);
217 wxComboWidgetImpl
* wxComboBox::GetComboPeer() const
219 return dynamic_cast<wxComboWidgetImpl
*> (GetPeer());
222 #endif // wxUSE_COMBOBOX && wxOSX_USE_COCOA