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 wxNSComboBox : NSComboBox
33 @implementation wxNSComboBox
37 static BOOL initialized = NO;
41 wxOSXCocoaClassAddWXMethods( self );
45 - (void)controlTextDidChange:(NSNotification *)aNotification
47 wxUnusedVar(aNotification);
48 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
49 if ( impl && impl->ShouldSendEvents() )
51 wxWindow* wxpeer = (wxWindow*) impl->GetWXPeer();
53 wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, wxpeer->GetId());
54 event.SetEventObject( wxpeer );
55 event.SetString( static_cast<wxComboBox*>(wxpeer)->GetValue() );
56 wxpeer->HandleWindowEvent( event );
61 - (void)comboBoxSelectionDidChange:(NSNotification *)notification
63 wxUnusedVar(notification);
64 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
65 if ( impl && impl->ShouldSendEvents())
67 wxWindow* wxpeer = (wxWindow*) impl->GetWXPeer();
69 wxCommandEvent event(wxEVT_COMMAND_COMBOBOX_SELECTED, wxpeer->GetId());
70 event.SetEventObject( wxpeer );
71 event.SetInt( static_cast<wxComboBox*>(wxpeer)->GetSelection() );
72 // For some reason, wxComboBox::GetValue will not return the newly selected item
73 // while we're inside this callback, so use AddPendingEvent to make sure
74 // GetValue() returns the right value.
75 wxpeer->GetEventHandler()->AddPendingEvent( event );
81 wxNSComboBoxControl::wxNSComboBoxControl( wxComboBox *wxPeer, WXWidget w )
82 : wxNSTextFieldControl(wxPeer, wxPeer, w)
84 m_comboBox = (NSComboBox*)w;
87 wxNSComboBoxControl::~wxNSComboBoxControl()
91 int wxNSComboBoxControl::GetSelectedItem() const
93 return [m_comboBox indexOfSelectedItem];
96 void wxNSComboBoxControl::SetSelectedItem(int item)
100 if ( item != wxNOT_FOUND )
102 wxASSERT_MSG( item >= 0 && item < [m_comboBox numberOfItems],
103 "Inavlid item index." );
104 [m_comboBox selectItemAtIndex: item];
106 else // remove current selection (if we have any)
108 const int sel = GetSelectedItem();
109 if ( sel != wxNOT_FOUND )
110 [m_comboBox deselectItemAtIndex:sel];
116 int wxNSComboBoxControl::GetNumberOfItems() const
118 return [m_comboBox numberOfItems];
121 void wxNSComboBoxControl::InsertItem(int pos, const wxString& item)
123 [m_comboBox insertItemWithObjectValue:wxCFStringRef( item , m_wxPeer->GetFont().GetEncoding() ).AsNSString() atIndex:pos];
126 void wxNSComboBoxControl::RemoveItem(int pos)
129 [m_comboBox removeItemAtIndex:pos];
133 void wxNSComboBoxControl::Clear()
136 [m_comboBox removeAllItems];
140 wxString wxNSComboBoxControl::GetStringAtIndex(int pos) const
142 return wxCFStringRef::AsString([m_comboBox itemObjectValueAtIndex:pos], m_wxPeer->GetFont().GetEncoding());
145 int wxNSComboBoxControl::FindString(const wxString& text) const
147 int result = [m_comboBox indexOfItemWithObjectValue:wxCFStringRef( text , m_wxPeer->GetFont().GetEncoding() ).AsNSString()];
148 if (result == NSNotFound)
149 result = wxNOT_FOUND;
153 wxWidgetImplType* wxWidgetImpl::CreateComboBox( wxComboBox* wxpeer,
154 wxWindowMac* WXUNUSED(parent),
155 wxWindowID WXUNUSED(id),
160 long WXUNUSED(extraStyle))
162 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
163 wxNSComboBox* v = [[wxNSComboBox alloc] initWithFrame:r];
164 if (style & wxCB_READONLY)
166 wxNSComboBoxControl* c = new wxNSComboBoxControl( wxpeer, v );
170 wxSize wxComboBox::DoGetBestSize() const
172 int lbWidth = GetCount() > 0 ? 20 : 100; // some defaults
173 wxSize baseSize = wxWindow::DoGetBestSize();
174 int lbHeight = baseSize.y;
178 wxClientDC dc(const_cast<wxComboBox*>(this));
180 // Find the widest line
181 for(unsigned int i = 0; i < GetCount(); i++)
183 wxString str(GetString(i));
185 wxCoord width, height ;
186 dc.GetTextExtent( str , &width, &height);
189 lbWidth = wxMax( lbWidth, wLine ) ;
192 // Add room for the popup arrow
193 lbWidth += 2 * lbHeight ;
196 return wxSize( lbWidth, lbHeight );
199 #endif // wxUSE_COMBOBOX