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"
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 @interface wxNSComboBox : NSComboBox
44 @implementation wxNSComboBox
48 static BOOL initialized = NO;
52 wxOSXCocoaClassAddWXMethods( self );
56 - (void)controlTextDidChange:(NSNotification *)aNotification
58 wxUnusedVar(aNotification);
59 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
60 if ( impl && impl->ShouldSendEvents() )
62 wxWindow* wxpeer = (wxWindow*) impl->GetWXPeer();
64 wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, wxpeer->GetId());
65 event.SetEventObject( wxpeer );
66 event.SetString( static_cast<wxComboBox*>(wxpeer)->GetValue() );
67 wxpeer->HandleWindowEvent( event );
72 - (void)comboBoxSelectionDidChange:(NSNotification *)notification
74 wxUnusedVar(notification);
75 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
76 if ( impl && impl->ShouldSendEvents())
78 wxWindow* wxpeer = (wxWindow*) impl->GetWXPeer();
80 wxCommandEvent event(wxEVT_COMMAND_COMBOBOX_SELECTED, wxpeer->GetId());
81 event.SetEventObject( wxpeer );
82 event.SetInt( static_cast<wxComboBox*>(wxpeer)->GetSelection() );
83 // For some reason, wxComboBox::GetValue will not return the newly selected item
84 // while we're inside this callback, so use AddPendingEvent to make sure
85 // GetValue() returns the right value.
86 wxpeer->GetEventHandler()->AddPendingEvent( event );
92 wxNSComboBoxControl::wxNSComboBoxControl( wxComboBox *wxPeer, WXWidget w )
93 : wxNSTextFieldControl(wxPeer, wxPeer, w)
95 m_comboBox = (NSComboBox*)w;
98 wxNSComboBoxControl::~wxNSComboBoxControl()
102 int wxNSComboBoxControl::GetSelectedItem() const
104 return [m_comboBox indexOfSelectedItem];
107 void wxNSComboBoxControl::SetSelectedItem(int item)
111 if ( item != wxNOT_FOUND )
113 wxASSERT_MSG( item >= 0 && item < [m_comboBox numberOfItems],
114 "Inavlid item index." );
115 [m_comboBox selectItemAtIndex: item];
117 else // remove current selection (if we have any)
119 const int sel = GetSelectedItem();
120 if ( sel != wxNOT_FOUND )
121 [m_comboBox deselectItemAtIndex:sel];
127 int wxNSComboBoxControl::GetNumberOfItems() const
129 return [m_comboBox numberOfItems];
132 void wxNSComboBoxControl::InsertItem(int pos, const wxString& item)
134 [m_comboBox insertItemWithObjectValue:wxCFStringRef( item , m_wxPeer->GetFont().GetEncoding() ).AsNSString() atIndex:pos];
137 void wxNSComboBoxControl::RemoveItem(int pos)
140 [m_comboBox removeItemAtIndex:pos];
144 void wxNSComboBoxControl::Clear()
147 [m_comboBox removeAllItems];
151 wxString wxNSComboBoxControl::GetStringAtIndex(int pos) const
153 return wxCFStringRef::AsString([m_comboBox itemObjectValueAtIndex:pos], m_wxPeer->GetFont().GetEncoding());
156 int wxNSComboBoxControl::FindString(const wxString& text) const
158 NSInteger nsresult = [m_comboBox indexOfItemWithObjectValue:wxCFStringRef( text , m_wxPeer->GetFont().GetEncoding() ).AsNSString()];
161 if (nsresult == NSNotFound)
162 result = wxNOT_FOUND;
164 result = (int) nsresult;
168 void wxNSComboBoxControl::Popup()
170 id ax = NSAccessibilityUnignoredDescendant(m_comboBox);
171 [ax accessibilitySetValue: [NSNumber numberWithBool: YES] forAttribute: NSAccessibilityExpandedAttribute];
174 void wxNSComboBoxControl::Dismiss()
176 id ax = NSAccessibilityUnignoredDescendant(m_comboBox);
177 [ax accessibilitySetValue: [NSNumber numberWithBool: NO] forAttribute: NSAccessibilityExpandedAttribute];
180 wxWidgetImplType* wxWidgetImpl::CreateComboBox( wxComboBox* wxpeer,
181 wxWindowMac* WXUNUSED(parent),
182 wxWindowID WXUNUSED(id),
183 wxMenu* WXUNUSED(menu),
187 long WXUNUSED(extraStyle))
189 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
190 wxNSComboBox* v = [[wxNSComboBox alloc] initWithFrame:r];
191 if (style & wxCB_READONLY)
193 wxNSComboBoxControl* c = new wxNSComboBoxControl( wxpeer, v );
197 wxSize wxComboBox::DoGetBestSize() const
199 int lbWidth = GetCount() > 0 ? 20 : 100; // some defaults
200 wxSize baseSize = wxWindow::DoGetBestSize();
201 int lbHeight = baseSize.y;
205 wxClientDC dc(const_cast<wxComboBox*>(this));
207 // Find the widest line
208 for(unsigned int i = 0; i < GetCount(); i++)
210 wxString str(GetString(i));
212 wxCoord width, height ;
213 dc.GetTextExtent( str , &width, &height);
216 lbWidth = wxMax( lbWidth, wLine ) ;
219 // Add room for the popup arrow
220 lbWidth += 2 * lbHeight ;
223 return wxSize( lbWidth, lbHeight );
226 #endif // wxUSE_COMBOBOX