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( wxWindow *wxPeer, WXWidget w ) : wxNSTextFieldControl(wxPeer, w)
83 m_comboBox = (NSComboBox*)w;
86 wxNSComboBoxControl::~wxNSComboBoxControl()
90 int wxNSComboBoxControl::GetSelectedItem() const
92 return [m_comboBox indexOfSelectedItem];
95 void wxNSComboBoxControl::SetSelectedItem(int item)
97 wxASSERT_MSG(item >= 0 && item < [m_comboBox numberOfItems], "Inavlid item index.");
99 [m_comboBox selectItemAtIndex: item];
103 int wxNSComboBoxControl::GetNumberOfItems() const
105 return [m_comboBox numberOfItems];
108 void wxNSComboBoxControl::InsertItem(int pos, const wxString& item)
110 [m_comboBox insertItemWithObjectValue:wxCFStringRef( item , m_wxPeer->GetFont().GetEncoding() ).AsNSString() atIndex:pos];
113 void wxNSComboBoxControl::RemoveItem(int pos)
116 [m_comboBox removeItemAtIndex:pos];
120 void wxNSComboBoxControl::Clear()
123 [m_comboBox removeAllItems];
127 wxString wxNSComboBoxControl::GetStringAtIndex(int pos) const
129 return wxCFStringRef::AsString([m_comboBox itemObjectValueAtIndex:pos], m_wxPeer->GetFont().GetEncoding());
132 int wxNSComboBoxControl::FindString(const wxString& text) const
134 int result = [m_comboBox indexOfItemWithObjectValue:wxCFStringRef( text , m_wxPeer->GetFont().GetEncoding() ).AsNSString()];
135 if (result == NSNotFound)
136 result = wxNOT_FOUND;
140 wxWidgetImplType* wxWidgetImpl::CreateComboBox( wxWindowMac* wxpeer,
141 wxWindowMac* WXUNUSED(parent),
142 wxWindowID WXUNUSED(id),
147 long WXUNUSED(extraStyle))
149 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
150 wxNSComboBox* v = [[wxNSComboBox alloc] initWithFrame:r];
151 if (style & wxCB_READONLY)
153 wxNSComboBoxControl* c = new wxNSComboBoxControl( wxpeer, v );
157 wxSize wxComboBox::DoGetBestSize() const
159 int lbWidth = GetCount() > 0 ? 20 : 100; // some defaults
160 wxSize baseSize = wxWindow::DoGetBestSize();
161 int lbHeight = baseSize.y;
165 wxClientDC dc(const_cast<wxComboBox*>(this));
167 // Find the widest line
168 for(unsigned int i = 0; i < GetCount(); i++)
170 wxString str(GetString(i));
172 wxCoord width, height ;
173 dc.GetTextExtent( str , &width, &height);
176 lbWidth = wxMax( lbWidth, wLine ) ;
179 // Add room for the popup arrow
180 lbWidth += 2 * lbHeight ;
183 return wxSize( lbWidth, lbHeight );
186 #endif // wxUSE_COMBOBOX