]> git.saurik.com Git - wxWidgets.git/blob - src/osx/cocoa/combobox.mm
Ignore non-existant string selection in wxComboBox::SetValue() in read-only mode...
[wxWidgets.git] / src / osx / cocoa / combobox.mm
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/cocoa/combobox.mm
3 // Purpose: wxChoice
4 // Author: Stefan Csomor
5 // Modified by:
6 // Created: 1998-01-01
7 // RCS-ID: $Id: combobox.mm 54129 2008-06-11 19:30:52Z SC $
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #include "wx/wxprec.h"
13
14 #if wxUSE_COMBOBOX
15
16 #include "wx/combobox.h"
17
18 #ifndef WX_PRECOMP
19 #include "wx/menu.h"
20 #include "wx/dcclient.h"
21 #endif
22
23 #include "wx/osx/cocoa/private/textimpl.h"
24
25 // work in progress
26
27 @interface wxNSComboBox : NSComboBox
28 {
29 }
30
31 @end
32
33 @implementation wxNSComboBox
34
35 + (void)initialize
36 {
37 static BOOL initialized = NO;
38 if (!initialized)
39 {
40 initialized = YES;
41 wxOSXCocoaClassAddWXMethods( self );
42 }
43 }
44
45 - (void)controlTextDidChange:(NSNotification *)aNotification
46 {
47 wxUnusedVar(aNotification);
48 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
49 if ( impl && impl->ShouldSendEvents() )
50 {
51 wxWindow* wxpeer = (wxWindow*) impl->GetWXPeer();
52 if ( wxpeer ) {
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 );
57 }
58 }
59 }
60
61 - (void)comboBoxSelectionDidChange:(NSNotification *)notification
62 {
63 wxUnusedVar(notification);
64 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
65 if ( impl && impl->ShouldSendEvents())
66 {
67 wxWindow* wxpeer = (wxWindow*) impl->GetWXPeer();
68 if ( wxpeer ) {
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 );
76 }
77 }
78 }
79 @end
80
81 wxNSComboBoxControl::wxNSComboBoxControl( wxComboBox *wxPeer, WXWidget w )
82 : wxNSTextFieldControl(wxPeer, wxPeer, w)
83 {
84 m_comboBox = (NSComboBox*)w;
85 }
86
87 wxNSComboBoxControl::~wxNSComboBoxControl()
88 {
89 }
90
91 int wxNSComboBoxControl::GetSelectedItem() const
92 {
93 return [m_comboBox indexOfSelectedItem];
94 }
95
96 void wxNSComboBoxControl::SetSelectedItem(int item)
97 {
98 wxASSERT_MSG(item >= 0 && item < [m_comboBox numberOfItems], "Inavlid item index.");
99 SendEvents(false);
100 [m_comboBox selectItemAtIndex: item];
101 SendEvents(true);
102 }
103
104 int wxNSComboBoxControl::GetNumberOfItems() const
105 {
106 return [m_comboBox numberOfItems];
107 }
108
109 void wxNSComboBoxControl::InsertItem(int pos, const wxString& item)
110 {
111 [m_comboBox insertItemWithObjectValue:wxCFStringRef( item , m_wxPeer->GetFont().GetEncoding() ).AsNSString() atIndex:pos];
112 }
113
114 void wxNSComboBoxControl::RemoveItem(int pos)
115 {
116 SendEvents(false);
117 [m_comboBox removeItemAtIndex:pos];
118 SendEvents(true);
119 }
120
121 void wxNSComboBoxControl::Clear()
122 {
123 SendEvents(false);
124 [m_comboBox removeAllItems];
125 SendEvents(true);
126 }
127
128 wxString wxNSComboBoxControl::GetStringAtIndex(int pos) const
129 {
130 return wxCFStringRef::AsString([m_comboBox itemObjectValueAtIndex:pos], m_wxPeer->GetFont().GetEncoding());
131 }
132
133 int wxNSComboBoxControl::FindString(const wxString& text) const
134 {
135 int result = [m_comboBox indexOfItemWithObjectValue:wxCFStringRef( text , m_wxPeer->GetFont().GetEncoding() ).AsNSString()];
136 if (result == NSNotFound)
137 result = wxNOT_FOUND;
138 return result;
139 }
140
141 wxWidgetImplType* wxWidgetImpl::CreateComboBox( wxComboBox* wxpeer,
142 wxWindowMac* WXUNUSED(parent),
143 wxWindowID WXUNUSED(id),
144 wxMenu* menu,
145 const wxPoint& pos,
146 const wxSize& size,
147 long style,
148 long WXUNUSED(extraStyle))
149 {
150 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
151 wxNSComboBox* v = [[wxNSComboBox alloc] initWithFrame:r];
152 if (style & wxCB_READONLY)
153 [v setEditable:NO];
154 wxNSComboBoxControl* c = new wxNSComboBoxControl( wxpeer, v );
155 return c;
156 }
157
158 wxSize wxComboBox::DoGetBestSize() const
159 {
160 int lbWidth = GetCount() > 0 ? 20 : 100; // some defaults
161 wxSize baseSize = wxWindow::DoGetBestSize();
162 int lbHeight = baseSize.y;
163 int wLine;
164
165 {
166 wxClientDC dc(const_cast<wxComboBox*>(this));
167
168 // Find the widest line
169 for(unsigned int i = 0; i < GetCount(); i++)
170 {
171 wxString str(GetString(i));
172
173 wxCoord width, height ;
174 dc.GetTextExtent( str , &width, &height);
175 wLine = width ;
176
177 lbWidth = wxMax( lbWidth, wLine ) ;
178 }
179
180 // Add room for the popup arrow
181 lbWidth += 2 * lbHeight ;
182 }
183
184 return wxSize( lbWidth, lbHeight );
185 }
186
187 #endif // wxUSE_COMBOBOX