]> git.saurik.com Git - wxWidgets.git/blob - src/osx/cocoa/combobox.mm
applying editor part of patch, see #15003
[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$
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 #include "wx/evtloop.h"
18
19 #ifndef WX_PRECOMP
20 #include "wx/menu.h"
21 #include "wx/dcclient.h"
22 #endif
23
24 #include "wx/osx/cocoa/private/textimpl.h"
25
26 // work in progress
27
28 @interface wxNSTableDataSource : NSObject wxOSX_10_6_AND_LATER(<NSComboBoxDataSource>)
29 {
30 wxNSComboBoxControl* impl;
31 }
32
33 - (NSInteger)numberOfItemsInComboBox:(NSComboBox *)aComboBox;
34 - (id)comboBox:(NSComboBox *)aComboBox objectValueForItemAtIndex:(NSInteger)index;
35
36 @end
37
38
39 @implementation wxNSComboBox
40
41 + (void)initialize
42 {
43 static BOOL initialized = NO;
44 if (!initialized)
45 {
46 initialized = YES;
47 wxOSXCocoaClassAddWXMethods( self );
48 }
49 }
50
51 - (void) dealloc
52 {
53 [fieldEditor release];
54 [super dealloc];
55 }
56
57 // Over-riding NSComboBox onKeyDown method doesn't work for key events.
58 // Ensure that we can use our own wxNSTextFieldEditor to catch key events.
59 // See windowWillReturnFieldEditor in nonownedwnd.mm.
60 // Key events will be caught and handled via wxNSTextFieldEditor onkey...
61 // methods in textctrl.mm.
62
63 - (void) setFieldEditor:(wxNSTextFieldEditor*) editor
64 {
65 if ( editor != fieldEditor )
66 {
67 [editor retain];
68 [fieldEditor release];
69 fieldEditor = editor;
70 }
71 }
72
73 - (wxNSTextFieldEditor*) fieldEditor
74 {
75 return fieldEditor;
76 }
77
78 - (void)controlTextDidChange:(NSNotification *)aNotification
79 {
80 wxUnusedVar(aNotification);
81 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
82 if ( impl && impl->ShouldSendEvents() )
83 {
84 wxWindow* wxpeer = (wxWindow*) impl->GetWXPeer();
85 if ( wxpeer ) {
86 wxCommandEvent event(wxEVT_TEXT, wxpeer->GetId());
87 event.SetEventObject( wxpeer );
88 event.SetString( static_cast<wxComboBox*>(wxpeer)->GetValue() );
89 wxpeer->HandleWindowEvent( event );
90 }
91 }
92 }
93
94 - (void)comboBoxSelectionDidChange:(NSNotification *)notification
95 {
96 wxUnusedVar(notification);
97 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
98 if ( impl && impl->ShouldSendEvents())
99 {
100 wxComboBox* wxpeer = static_cast<wxComboBox*>(impl->GetWXPeer());
101 if ( wxpeer ) {
102 const int sel = wxpeer->GetSelection();
103
104 wxCommandEvent event(wxEVT_COMBOBOX, wxpeer->GetId());
105 event.SetEventObject( wxpeer );
106 event.SetInt( sel );
107 event.SetString( wxpeer->GetString(sel) );
108 // For some reason, wxComboBox::GetValue will not return the newly selected item
109 // while we're inside this callback, so use AddPendingEvent to make sure
110 // GetValue() returns the right value.
111 wxEventLoop* const loop = (wxEventLoop*) wxEventLoopBase::GetActive();
112 if ( loop )
113 loop->OSXUseLowLevelWakeup(true);
114
115 wxpeer->GetEventHandler()->AddPendingEvent( event );
116
117 if ( loop )
118 loop->OSXUseLowLevelWakeup(false);
119 }
120 }
121 }
122 @end
123
124 wxNSComboBoxControl::wxNSComboBoxControl( wxComboBox *wxPeer, WXWidget w )
125 : wxNSTextFieldControl(wxPeer, wxPeer, w)
126 {
127 m_comboBox = (NSComboBox*)w;
128 }
129
130 wxNSComboBoxControl::~wxNSComboBoxControl()
131 {
132 }
133
134 int wxNSComboBoxControl::GetSelectedItem() const
135 {
136 return [m_comboBox indexOfSelectedItem];
137 }
138
139 void wxNSComboBoxControl::SetSelectedItem(int item)
140 {
141 SendEvents(false);
142
143 if ( item != wxNOT_FOUND )
144 {
145 wxASSERT_MSG( item >= 0 && item < [m_comboBox numberOfItems],
146 "Inavlid item index." );
147 [m_comboBox selectItemAtIndex: item];
148 }
149 else // remove current selection (if we have any)
150 {
151 const int sel = GetSelectedItem();
152 if ( sel != wxNOT_FOUND )
153 [m_comboBox deselectItemAtIndex:sel];
154 }
155
156 SendEvents(true);
157 }
158
159 int wxNSComboBoxControl::GetNumberOfItems() const
160 {
161 return [m_comboBox numberOfItems];
162 }
163
164 void wxNSComboBoxControl::InsertItem(int pos, const wxString& item)
165 {
166 [m_comboBox insertItemWithObjectValue:wxCFStringRef( item , m_wxPeer->GetFont().GetEncoding() ).AsNSString() atIndex:pos];
167 }
168
169 void wxNSComboBoxControl::RemoveItem(int pos)
170 {
171 SendEvents(false);
172 [m_comboBox removeItemAtIndex:pos];
173 SendEvents(true);
174 }
175
176 void wxNSComboBoxControl::Clear()
177 {
178 SendEvents(false);
179 [m_comboBox removeAllItems];
180 SendEvents(true);
181 }
182
183 wxString wxNSComboBoxControl::GetStringAtIndex(int pos) const
184 {
185 return wxCFStringRef::AsString([m_comboBox itemObjectValueAtIndex:pos], m_wxPeer->GetFont().GetEncoding());
186 }
187
188 int wxNSComboBoxControl::FindString(const wxString& text) const
189 {
190 NSInteger nsresult = [m_comboBox indexOfItemWithObjectValue:wxCFStringRef( text , m_wxPeer->GetFont().GetEncoding() ).AsNSString()];
191
192 int result;
193 if (nsresult == NSNotFound)
194 result = wxNOT_FOUND;
195 else
196 result = (int) nsresult;
197 return result;
198 }
199
200 void wxNSComboBoxControl::Popup()
201 {
202 id ax = NSAccessibilityUnignoredDescendant(m_comboBox);
203 [ax accessibilitySetValue: [NSNumber numberWithBool: YES] forAttribute: NSAccessibilityExpandedAttribute];
204 }
205
206 void wxNSComboBoxControl::Dismiss()
207 {
208 id ax = NSAccessibilityUnignoredDescendant(m_comboBox);
209 [ax accessibilitySetValue: [NSNumber numberWithBool: NO] forAttribute: NSAccessibilityExpandedAttribute];
210 }
211
212 wxWidgetImplType* wxWidgetImpl::CreateComboBox( wxComboBox* wxpeer,
213 wxWindowMac* WXUNUSED(parent),
214 wxWindowID WXUNUSED(id),
215 wxMenu* WXUNUSED(menu),
216 const wxPoint& pos,
217 const wxSize& size,
218 long style,
219 long WXUNUSED(extraStyle))
220 {
221 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
222 wxNSComboBox* v = [[wxNSComboBox alloc] initWithFrame:r];
223 if (style & wxCB_READONLY)
224 [v setEditable:NO];
225 wxNSComboBoxControl* c = new wxNSComboBoxControl( wxpeer, v );
226 return c;
227 }
228
229 wxSize wxComboBox::DoGetBestSize() const
230 {
231 int lbWidth = GetCount() > 0 ? 20 : 100; // some defaults
232 wxSize baseSize = wxWindow::DoGetBestSize();
233 int lbHeight = baseSize.y;
234 int wLine;
235
236 {
237 wxClientDC dc(const_cast<wxComboBox*>(this));
238
239 // Find the widest line
240 for(unsigned int i = 0; i < GetCount(); i++)
241 {
242 wxString str(GetString(i));
243
244 wxCoord width, height ;
245 dc.GetTextExtent( str , &width, &height);
246 wLine = width ;
247
248 lbWidth = wxMax( lbWidth, wLine ) ;
249 }
250
251 // Add room for the popup arrow
252 lbWidth += 2 * lbHeight ;
253 }
254
255 return wxSize( lbWidth, lbHeight );
256 }
257
258 #endif // wxUSE_COMBOBOX