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 @interface wxNSComboBox : NSComboBox
45 @implementation wxNSComboBox
49 static BOOL initialized = NO;
53 wxOSXCocoaClassAddWXMethods( self );
57 - (void)controlTextDidChange:(NSNotification *)aNotification
59 wxUnusedVar(aNotification);
60 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
61 if ( impl && impl->ShouldSendEvents() )
63 wxWindow* wxpeer = (wxWindow*) impl->GetWXPeer();
65 wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, wxpeer->GetId());
66 event.SetEventObject( wxpeer );
67 event.SetString( static_cast<wxComboBox*>(wxpeer)->GetValue() );
68 wxpeer->HandleWindowEvent( event );
73 - (void)comboBoxSelectionDidChange:(NSNotification *)notification
75 wxUnusedVar(notification);
76 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
77 if ( impl && impl->ShouldSendEvents())
79 wxComboBox* wxpeer = static_cast<wxComboBox*>(impl->GetWXPeer());
81 const int sel = wxpeer->GetSelection();
83 wxCommandEvent event(wxEVT_COMMAND_COMBOBOX_SELECTED, wxpeer->GetId());
84 event.SetEventObject( wxpeer );
86 event.SetString( wxpeer->GetString(sel) );
87 // For some reason, wxComboBox::GetValue will not return the newly selected item
88 // while we're inside this callback, so use AddPendingEvent to make sure
89 // GetValue() returns the right value.
90 wxEventLoop* const loop = (wxEventLoop*) wxEventLoopBase::GetActive();
92 loop->OSXUseLowLevelWakeup(true);
94 wxpeer->GetEventHandler()->AddPendingEvent( event );
97 loop->OSXUseLowLevelWakeup(false);
103 wxNSComboBoxControl::wxNSComboBoxControl( wxComboBox *wxPeer, WXWidget w )
104 : wxNSTextFieldControl(wxPeer, wxPeer, w)
106 m_comboBox = (NSComboBox*)w;
109 wxNSComboBoxControl::~wxNSComboBoxControl()
113 int wxNSComboBoxControl::GetSelectedItem() const
115 return [m_comboBox indexOfSelectedItem];
118 void wxNSComboBoxControl::SetSelectedItem(int item)
122 if ( item != wxNOT_FOUND )
124 wxASSERT_MSG( item >= 0 && item < [m_comboBox numberOfItems],
125 "Inavlid item index." );
126 [m_comboBox selectItemAtIndex: item];
128 else // remove current selection (if we have any)
130 const int sel = GetSelectedItem();
131 if ( sel != wxNOT_FOUND )
132 [m_comboBox deselectItemAtIndex:sel];
138 int wxNSComboBoxControl::GetNumberOfItems() const
140 return [m_comboBox numberOfItems];
143 void wxNSComboBoxControl::InsertItem(int pos, const wxString& item)
145 [m_comboBox insertItemWithObjectValue:wxCFStringRef( item , m_wxPeer->GetFont().GetEncoding() ).AsNSString() atIndex:pos];
148 void wxNSComboBoxControl::RemoveItem(int pos)
151 [m_comboBox removeItemAtIndex:pos];
155 void wxNSComboBoxControl::Clear()
158 [m_comboBox removeAllItems];
162 wxString wxNSComboBoxControl::GetStringAtIndex(int pos) const
164 return wxCFStringRef::AsString([m_comboBox itemObjectValueAtIndex:pos], m_wxPeer->GetFont().GetEncoding());
167 int wxNSComboBoxControl::FindString(const wxString& text) const
169 NSInteger nsresult = [m_comboBox indexOfItemWithObjectValue:wxCFStringRef( text , m_wxPeer->GetFont().GetEncoding() ).AsNSString()];
172 if (nsresult == NSNotFound)
173 result = wxNOT_FOUND;
175 result = (int) nsresult;
179 void wxNSComboBoxControl::Popup()
181 id ax = NSAccessibilityUnignoredDescendant(m_comboBox);
182 [ax accessibilitySetValue: [NSNumber numberWithBool: YES] forAttribute: NSAccessibilityExpandedAttribute];
185 void wxNSComboBoxControl::Dismiss()
187 id ax = NSAccessibilityUnignoredDescendant(m_comboBox);
188 [ax accessibilitySetValue: [NSNumber numberWithBool: NO] forAttribute: NSAccessibilityExpandedAttribute];
191 wxWidgetImplType* wxWidgetImpl::CreateComboBox( wxComboBox* wxpeer,
192 wxWindowMac* WXUNUSED(parent),
193 wxWindowID WXUNUSED(id),
194 wxMenu* WXUNUSED(menu),
198 long WXUNUSED(extraStyle))
200 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
201 wxNSComboBox* v = [[wxNSComboBox alloc] initWithFrame:r];
202 if (style & wxCB_READONLY)
204 wxNSComboBoxControl* c = new wxNSComboBoxControl( wxpeer, v );
208 wxSize wxComboBox::DoGetBestSize() const
210 int lbWidth = GetCount() > 0 ? 20 : 100; // some defaults
211 wxSize baseSize = wxWindow::DoGetBestSize();
212 int lbHeight = baseSize.y;
216 wxClientDC dc(const_cast<wxComboBox*>(this));
218 // Find the widest line
219 for(unsigned int i = 0; i < GetCount(); i++)
221 wxString str(GetString(i));
223 wxCoord width, height ;
224 dc.GetTextExtent( str , &width, &height);
227 lbWidth = wxMax( lbWidth, wLine ) ;
230 // Add room for the popup arrow
231 lbWidth += 2 * lbHeight ;
234 return wxSize( lbWidth, lbHeight );
237 #endif // wxUSE_COMBOBOX