1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/cocoa/combobox.mm
4 // Author: Stefan Csomor
7 // RCS-ID: $Id: combobox.mm 54129 2008-06-11 19:30:52Z SC $
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 wxWidgetImplType* wxWidgetImpl::CreateComboBox( wxComboBox* wxpeer,
169 wxWindowMac* WXUNUSED(parent),
170 wxWindowID WXUNUSED(id),
171 wxMenu* WXUNUSED(menu),
175 long WXUNUSED(extraStyle))
177 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
178 wxNSComboBox* v = [[wxNSComboBox alloc] initWithFrame:r];
179 if (style & wxCB_READONLY)
181 wxNSComboBoxControl* c = new wxNSComboBoxControl( wxpeer, v );
185 wxSize wxComboBox::DoGetBestSize() const
187 int lbWidth = GetCount() > 0 ? 20 : 100; // some defaults
188 wxSize baseSize = wxWindow::DoGetBestSize();
189 int lbHeight = baseSize.y;
193 wxClientDC dc(const_cast<wxComboBox*>(this));
195 // Find the widest line
196 for(unsigned int i = 0; i < GetCount(); i++)
198 wxString str(GetString(i));
200 wxCoord width, height ;
201 dc.GetTextExtent( str , &width, &height);
204 lbWidth = wxMax( lbWidth, wLine ) ;
207 // Add room for the popup arrow
208 lbWidth += 2 * lbHeight ;
211 return wxSize( lbWidth, lbHeight );
214 #endif // wxUSE_COMBOBOX