1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/cocoa/combobox.mm
4 // Author: Stefan Csomor
7 // Copyright: (c) Stefan Csomor
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 #include "wx/wxprec.h"
15 #include "wx/combobox.h"
16 #include "wx/evtloop.h"
20 #include "wx/dcclient.h"
23 #include "wx/osx/cocoa/private/textimpl.h"
27 @interface wxNSTableDataSource : NSObject wxOSX_10_6_AND_LATER(<NSComboBoxDataSource>)
29 wxNSComboBoxControl* impl;
32 - (NSInteger)numberOfItemsInComboBox:(NSComboBox *)aComboBox;
33 - (id)comboBox:(NSComboBox *)aComboBox objectValueForItemAtIndex:(NSInteger)index;
38 @implementation wxNSComboBox
42 static BOOL initialized = NO;
46 wxOSXCocoaClassAddWXMethods( self );
52 [fieldEditor release];
56 // Over-riding NSComboBox onKeyDown method doesn't work for key events.
57 // Ensure that we can use our own wxNSTextFieldEditor to catch key events.
58 // See windowWillReturnFieldEditor in nonownedwnd.mm.
59 // Key events will be caught and handled via wxNSTextFieldEditor onkey...
60 // methods in textctrl.mm.
62 - (void) setFieldEditor:(wxNSTextFieldEditor*) editor
64 if ( editor != fieldEditor )
67 [fieldEditor release];
72 - (wxNSTextFieldEditor*) fieldEditor
77 - (void)controlTextDidChange:(NSNotification *)aNotification
79 wxUnusedVar(aNotification);
80 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
81 if ( impl && impl->ShouldSendEvents() )
83 wxWindow* wxpeer = (wxWindow*) impl->GetWXPeer();
85 wxCommandEvent event(wxEVT_TEXT, wxpeer->GetId());
86 event.SetEventObject( wxpeer );
87 event.SetString( static_cast<wxComboBox*>(wxpeer)->GetValue() );
88 wxpeer->HandleWindowEvent( event );
93 - (void)comboBoxSelectionDidChange:(NSNotification *)notification
95 wxUnusedVar(notification);
96 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
97 if ( impl && impl->ShouldSendEvents())
99 wxComboBox* wxpeer = static_cast<wxComboBox*>(impl->GetWXPeer());
101 const int sel = wxpeer->GetSelection();
103 wxCommandEvent event(wxEVT_COMBOBOX, wxpeer->GetId());
104 event.SetEventObject( wxpeer );
106 event.SetString( wxpeer->GetString(sel) );
107 // For some reason, wxComboBox::GetValue will not return the newly selected item
108 // while we're inside this callback, so use AddPendingEvent to make sure
109 // GetValue() returns the right value.
111 wxpeer->GetEventHandler()->AddPendingEvent( event );
118 wxNSComboBoxControl::wxNSComboBoxControl( wxComboBox *wxPeer, WXWidget w )
119 : wxNSTextFieldControl(wxPeer, wxPeer, w)
121 m_comboBox = (NSComboBox*)w;
124 wxNSComboBoxControl::~wxNSComboBoxControl()
128 void wxNSComboBoxControl::mouseEvent(WX_NSEvent event, WXWidget slf, void *_cmd)
130 // NSComboBox has its own event loop, which reacts very badly to our synthetic
131 // events used to signal when a wxEvent is posted, so during that time we switch
132 // the wxEventLoop::WakeUp implementation to a lower-level version
135 wxEventLoop* const loop = (wxEventLoop*) wxEventLoopBase::GetActive();
137 if ( loop != NULL && [event type] == NSLeftMouseDown )
140 loop->OSXUseLowLevelWakeup(true);
143 wxOSX_EventHandlerPtr superimpl = (wxOSX_EventHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
144 superimpl(slf, (SEL)_cmd, event);
148 loop->OSXUseLowLevelWakeup(false);
152 int wxNSComboBoxControl::GetSelectedItem() const
154 return [m_comboBox indexOfSelectedItem];
157 void wxNSComboBoxControl::SetSelectedItem(int item)
161 if ( item != wxNOT_FOUND )
163 wxASSERT_MSG( item >= 0 && item < [m_comboBox numberOfItems],
164 "Inavlid item index." );
165 [m_comboBox selectItemAtIndex: item];
167 else // remove current selection (if we have any)
169 const int sel = GetSelectedItem();
170 if ( sel != wxNOT_FOUND )
171 [m_comboBox deselectItemAtIndex:sel];
177 int wxNSComboBoxControl::GetNumberOfItems() const
179 return [m_comboBox numberOfItems];
182 void wxNSComboBoxControl::InsertItem(int pos, const wxString& item)
184 [m_comboBox insertItemWithObjectValue:wxCFStringRef( item , m_wxPeer->GetFont().GetEncoding() ).AsNSString() atIndex:pos];
187 void wxNSComboBoxControl::RemoveItem(int pos)
190 [m_comboBox removeItemAtIndex:pos];
194 void wxNSComboBoxControl::Clear()
197 [m_comboBox removeAllItems];
198 [m_comboBox setStringValue:@""];
202 wxString wxNSComboBoxControl::GetStringAtIndex(int pos) const
204 return wxCFStringRef::AsString([m_comboBox itemObjectValueAtIndex:pos], m_wxPeer->GetFont().GetEncoding());
207 int wxNSComboBoxControl::FindString(const wxString& text) const
209 NSInteger nsresult = [m_comboBox indexOfItemWithObjectValue:wxCFStringRef( text , m_wxPeer->GetFont().GetEncoding() ).AsNSString()];
212 if (nsresult == NSNotFound)
213 result = wxNOT_FOUND;
215 result = (int) nsresult;
219 void wxNSComboBoxControl::Popup()
221 id ax = NSAccessibilityUnignoredDescendant(m_comboBox);
222 [ax accessibilitySetValue: [NSNumber numberWithBool: YES] forAttribute: NSAccessibilityExpandedAttribute];
225 void wxNSComboBoxControl::Dismiss()
227 id ax = NSAccessibilityUnignoredDescendant(m_comboBox);
228 [ax accessibilitySetValue: [NSNumber numberWithBool: NO] forAttribute: NSAccessibilityExpandedAttribute];
231 void wxNSComboBoxControl::SetEditable(bool editable)
233 // TODO: unfortunately this does not work, setEditable just means the same as CB_READONLY
234 // I don't see a way to access the text field directly
236 // Behavior NONE <- SELECTECTABLE
237 [m_comboBox setEditable:editable];
240 wxWidgetImplType* wxWidgetImpl::CreateComboBox( wxComboBox* wxpeer,
241 wxWindowMac* WXUNUSED(parent),
242 wxWindowID WXUNUSED(id),
243 wxMenu* WXUNUSED(menu),
247 long WXUNUSED(extraStyle))
249 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
250 wxNSComboBox* v = [[wxNSComboBox alloc] initWithFrame:r];
251 if (style & wxCB_READONLY)
253 [v setNumberOfVisibleItems:10];
254 wxNSComboBoxControl* c = new wxNSComboBoxControl( wxpeer, v );
258 wxSize wxComboBox::DoGetBestSize() const
260 int lbWidth = GetCount() > 0 ? 20 : 100; // some defaults
261 wxSize baseSize = wxWindow::DoGetBestSize();
262 int lbHeight = baseSize.y;
266 wxClientDC dc(const_cast<wxComboBox*>(this));
268 // Find the widest line
269 for(unsigned int i = 0; i < GetCount(); i++)
271 wxString str(GetString(i));
273 wxCoord width, height ;
274 dc.GetTextExtent( str , &width, &height);
277 lbWidth = wxMax( lbWidth, wLine ) ;
280 // Add room for the popup arrow
281 lbWidth += 2 * lbHeight ;
284 return wxSize( lbWidth, lbHeight );
287 #endif // wxUSE_COMBOBOX