1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/cocoa/combobox.mm
4 // Author: Stefan Csomor
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
16 #include "wx/combobox.h"
17 #include "wx/evtloop.h"
21 #include "wx/dcclient.h"
24 #include "wx/osx/cocoa/private/textimpl.h"
28 @interface wxNSTableDataSource : NSObject wxOSX_10_6_AND_LATER(<NSComboBoxDataSource>)
30 wxNSComboBoxControl* impl;
33 - (NSInteger)numberOfItemsInComboBox:(NSComboBox *)aComboBox;
34 - (id)comboBox:(NSComboBox *)aComboBox objectValueForItemAtIndex:(NSInteger)index;
39 @implementation wxNSComboBox
43 static BOOL initialized = NO;
47 wxOSXCocoaClassAddWXMethods( self );
53 [fieldEditor release];
57 // Over-riding NSComboBox onKeyDown method doesn't work for key events.
58 // Ensure that we can use our own wxNSTextFieldEditor to catch key events.
59 // See windowWillReturnFieldEditor in nonownedwnd.mm.
60 // Key events will be caught and handled via wxNSTextFieldEditor onkey...
61 // methods in textctrl.mm.
63 - (void) setFieldEditor:(wxNSTextFieldEditor*) editor
65 if ( editor != fieldEditor )
68 [fieldEditor release];
73 - (wxNSTextFieldEditor*) fieldEditor
78 - (void)controlTextDidChange:(NSNotification *)aNotification
80 wxUnusedVar(aNotification);
81 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
82 if ( impl && impl->ShouldSendEvents() )
84 wxWindow* wxpeer = (wxWindow*) impl->GetWXPeer();
86 wxCommandEvent event(wxEVT_TEXT, wxpeer->GetId());
87 event.SetEventObject( wxpeer );
88 event.SetString( static_cast<wxComboBox*>(wxpeer)->GetValue() );
89 wxpeer->HandleWindowEvent( event );
94 - (void)comboBoxSelectionDidChange:(NSNotification *)notification
96 wxUnusedVar(notification);
97 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
98 if ( impl && impl->ShouldSendEvents())
100 wxComboBox* wxpeer = static_cast<wxComboBox*>(impl->GetWXPeer());
102 const int sel = wxpeer->GetSelection();
104 wxCommandEvent event(wxEVT_COMBOBOX, wxpeer->GetId());
105 event.SetEventObject( wxpeer );
107 event.SetString( wxpeer->GetString(sel) );
108 // For some reason, wxComboBox::GetValue will not return the newly selected item
109 // while we're inside this callback, so use AddPendingEvent to make sure
110 // GetValue() returns the right value.
111 wxEventLoop* const loop = (wxEventLoop*) wxEventLoopBase::GetActive();
113 loop->OSXUseLowLevelWakeup(true);
115 wxpeer->GetEventHandler()->AddPendingEvent( event );
118 loop->OSXUseLowLevelWakeup(false);
124 wxNSComboBoxControl::wxNSComboBoxControl( wxComboBox *wxPeer, WXWidget w )
125 : wxNSTextFieldControl(wxPeer, wxPeer, w)
127 m_comboBox = (NSComboBox*)w;
130 wxNSComboBoxControl::~wxNSComboBoxControl()
134 int wxNSComboBoxControl::GetSelectedItem() const
136 return [m_comboBox indexOfSelectedItem];
139 void wxNSComboBoxControl::SetSelectedItem(int item)
143 if ( item != wxNOT_FOUND )
145 wxASSERT_MSG( item >= 0 && item < [m_comboBox numberOfItems],
146 "Inavlid item index." );
147 [m_comboBox selectItemAtIndex: item];
149 else // remove current selection (if we have any)
151 const int sel = GetSelectedItem();
152 if ( sel != wxNOT_FOUND )
153 [m_comboBox deselectItemAtIndex:sel];
159 int wxNSComboBoxControl::GetNumberOfItems() const
161 return [m_comboBox numberOfItems];
164 void wxNSComboBoxControl::InsertItem(int pos, const wxString& item)
166 [m_comboBox insertItemWithObjectValue:wxCFStringRef( item , m_wxPeer->GetFont().GetEncoding() ).AsNSString() atIndex:pos];
169 void wxNSComboBoxControl::RemoveItem(int pos)
172 [m_comboBox removeItemAtIndex:pos];
176 void wxNSComboBoxControl::Clear()
179 [m_comboBox removeAllItems];
180 [m_comboBox setStringValue:@""];
184 wxString wxNSComboBoxControl::GetStringAtIndex(int pos) const
186 return wxCFStringRef::AsString([m_comboBox itemObjectValueAtIndex:pos], m_wxPeer->GetFont().GetEncoding());
189 int wxNSComboBoxControl::FindString(const wxString& text) const
191 NSInteger nsresult = [m_comboBox indexOfItemWithObjectValue:wxCFStringRef( text , m_wxPeer->GetFont().GetEncoding() ).AsNSString()];
194 if (nsresult == NSNotFound)
195 result = wxNOT_FOUND;
197 result = (int) nsresult;
201 void wxNSComboBoxControl::Popup()
203 id ax = NSAccessibilityUnignoredDescendant(m_comboBox);
204 [ax accessibilitySetValue: [NSNumber numberWithBool: YES] forAttribute: NSAccessibilityExpandedAttribute];
207 void wxNSComboBoxControl::Dismiss()
209 id ax = NSAccessibilityUnignoredDescendant(m_comboBox);
210 [ax accessibilitySetValue: [NSNumber numberWithBool: NO] forAttribute: NSAccessibilityExpandedAttribute];
213 void wxNSComboBoxControl::SetEditable(bool editable)
215 // TODO: unfortunately this does not work, setEditable just means the same as CB_READONLY
216 // I don't see a way to access the text field directly
217 NSComboBoxCell* c = [m_comboBox cell];
218 [c setEditable:editable];
221 wxWidgetImplType* wxWidgetImpl::CreateComboBox( wxComboBox* wxpeer,
222 wxWindowMac* WXUNUSED(parent),
223 wxWindowID WXUNUSED(id),
224 wxMenu* WXUNUSED(menu),
228 long WXUNUSED(extraStyle))
230 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
231 wxNSComboBox* v = [[wxNSComboBox alloc] initWithFrame:r];
232 if (style & wxCB_READONLY)
234 wxNSComboBoxControl* c = new wxNSComboBoxControl( wxpeer, v );
238 wxSize wxComboBox::DoGetBestSize() const
240 int lbWidth = GetCount() > 0 ? 20 : 100; // some defaults
241 wxSize baseSize = wxWindow::DoGetBestSize();
242 int lbHeight = baseSize.y;
246 wxClientDC dc(const_cast<wxComboBox*>(this));
248 // Find the widest line
249 for(unsigned int i = 0; i < GetCount(); i++)
251 wxString str(GetString(i));
253 wxCoord width, height ;
254 dc.GetTextExtent( str , &width, &height);
257 lbWidth = wxMax( lbWidth, wLine ) ;
260 // Add room for the popup arrow
261 lbWidth += 2 * lbHeight ;
264 return wxSize( lbWidth, lbHeight );
267 #endif // wxUSE_COMBOBOX