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 wxWindow* wxpeer = (wxWindow*) impl->GetWXPeer();
81 wxCommandEvent event(wxEVT_COMMAND_COMBOBOX_SELECTED, wxpeer->GetId());
82 event.SetEventObject( wxpeer );
83 event.SetInt( static_cast<wxComboBox*>(wxpeer)->GetSelection() );
84 // For some reason, wxComboBox::GetValue will not return the newly selected item
85 // while we're inside this callback, so use AddPendingEvent to make sure
86 // GetValue() returns the right value.
87 wxEventLoop* const loop = (wxEventLoop*) wxEventLoopBase::GetActive();
89 loop->OSXUseLowLevelWakeup(true);
91 wxpeer->GetEventHandler()->AddPendingEvent( event );
94 loop->OSXUseLowLevelWakeup(false);
100 wxNSComboBoxControl::wxNSComboBoxControl( wxComboBox *wxPeer, WXWidget w )
101 : wxNSTextFieldControl(wxPeer, wxPeer, w)
103 m_comboBox = (NSComboBox*)w;
106 wxNSComboBoxControl::~wxNSComboBoxControl()
110 int wxNSComboBoxControl::GetSelectedItem() const
112 return [m_comboBox indexOfSelectedItem];
115 void wxNSComboBoxControl::SetSelectedItem(int item)
119 if ( item != wxNOT_FOUND )
121 wxASSERT_MSG( item >= 0 && item < [m_comboBox numberOfItems],
122 "Inavlid item index." );
123 [m_comboBox selectItemAtIndex: item];
125 else // remove current selection (if we have any)
127 const int sel = GetSelectedItem();
128 if ( sel != wxNOT_FOUND )
129 [m_comboBox deselectItemAtIndex:sel];
135 int wxNSComboBoxControl::GetNumberOfItems() const
137 return [m_comboBox numberOfItems];
140 void wxNSComboBoxControl::InsertItem(int pos, const wxString& item)
142 [m_comboBox insertItemWithObjectValue:wxCFStringRef( item , m_wxPeer->GetFont().GetEncoding() ).AsNSString() atIndex:pos];
145 void wxNSComboBoxControl::RemoveItem(int pos)
148 [m_comboBox removeItemAtIndex:pos];
152 void wxNSComboBoxControl::Clear()
155 [m_comboBox removeAllItems];
159 wxString wxNSComboBoxControl::GetStringAtIndex(int pos) const
161 return wxCFStringRef::AsString([m_comboBox itemObjectValueAtIndex:pos], m_wxPeer->GetFont().GetEncoding());
164 int wxNSComboBoxControl::FindString(const wxString& text) const
166 NSInteger nsresult = [m_comboBox indexOfItemWithObjectValue:wxCFStringRef( text , m_wxPeer->GetFont().GetEncoding() ).AsNSString()];
169 if (nsresult == NSNotFound)
170 result = wxNOT_FOUND;
172 result = (int) nsresult;
176 void wxNSComboBoxControl::Popup()
178 id ax = NSAccessibilityUnignoredDescendant(m_comboBox);
179 [ax accessibilitySetValue: [NSNumber numberWithBool: YES] forAttribute: NSAccessibilityExpandedAttribute];
182 void wxNSComboBoxControl::Dismiss()
184 id ax = NSAccessibilityUnignoredDescendant(m_comboBox);
185 [ax accessibilitySetValue: [NSNumber numberWithBool: NO] forAttribute: NSAccessibilityExpandedAttribute];
188 wxWidgetImplType* wxWidgetImpl::CreateComboBox( wxComboBox* wxpeer,
189 wxWindowMac* WXUNUSED(parent),
190 wxWindowID WXUNUSED(id),
191 wxMenu* WXUNUSED(menu),
195 long WXUNUSED(extraStyle))
197 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
198 wxNSComboBox* v = [[wxNSComboBox alloc] initWithFrame:r];
199 if (style & wxCB_READONLY)
201 wxNSComboBoxControl* c = new wxNSComboBoxControl( wxpeer, v );
205 wxSize wxComboBox::DoGetBestSize() const
207 int lbWidth = GetCount() > 0 ? 20 : 100; // some defaults
208 wxSize baseSize = wxWindow::DoGetBestSize();
209 int lbHeight = baseSize.y;
213 wxClientDC dc(const_cast<wxComboBox*>(this));
215 // Find the widest line
216 for(unsigned int i = 0; i < GetCount(); i++)
218 wxString str(GetString(i));
220 wxCoord width, height ;
221 dc.GetTextExtent( str , &width, &height);
224 lbWidth = wxMax( lbWidth, wLine ) ;
227 // Add room for the popup arrow
228 lbWidth += 2 * lbHeight ;
231 return wxSize( lbWidth, lbHeight );
234 #endif // wxUSE_COMBOBOX