]> git.saurik.com Git - wxWidgets.git/blob - src/osx/cocoa/combobox.mm
use explicit WakeUp variant, too many errors using heuristics, fixes #14176
[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 @interface wxNSComboBox : NSComboBox
40 {
41 }
42
43 @end
44
45 @implementation wxNSComboBox
46
47 + (void)initialize
48 {
49 static BOOL initialized = NO;
50 if (!initialized)
51 {
52 initialized = YES;
53 wxOSXCocoaClassAddWXMethods( self );
54 }
55 }
56
57 - (void)controlTextDidChange:(NSNotification *)aNotification
58 {
59 wxUnusedVar(aNotification);
60 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
61 if ( impl && impl->ShouldSendEvents() )
62 {
63 wxWindow* wxpeer = (wxWindow*) impl->GetWXPeer();
64 if ( wxpeer ) {
65 wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, wxpeer->GetId());
66 event.SetEventObject( wxpeer );
67 event.SetString( static_cast<wxComboBox*>(wxpeer)->GetValue() );
68 wxpeer->HandleWindowEvent( event );
69 }
70 }
71 }
72
73 - (void)comboBoxSelectionDidChange:(NSNotification *)notification
74 {
75 wxUnusedVar(notification);
76 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
77 if ( impl && impl->ShouldSendEvents())
78 {
79 wxWindow* wxpeer = (wxWindow*) impl->GetWXPeer();
80 if ( wxpeer ) {
81 wxCommandEvent event(wxEVT_COMMAND_COMBOBOX_SELECTED, wxpeer->GetId());
82 event.SetEventObject( wxpeer );
83 event.SetInt( static_cast<wxComboBox*>(wxpeer)->GetSelection() );
84 // For some reason, wxComboBox::GetValue will not return the newly selected item
85 // while we're inside this callback, so use AddPendingEvent to make sure
86 // GetValue() returns the right value.
87 wxEventLoop* const loop = (wxEventLoop*) wxEventLoopBase::GetActive();
88 if ( loop )
89 loop->OSXUseLowLevelWakeup(true);
90
91 wxpeer->GetEventHandler()->AddPendingEvent( event );
92
93 if ( loop )
94 loop->OSXUseLowLevelWakeup(false);
95 }
96 }
97 }
98 @end
99
100 wxNSComboBoxControl::wxNSComboBoxControl( wxComboBox *wxPeer, WXWidget w )
101 : wxNSTextFieldControl(wxPeer, wxPeer, w)
102 {
103 m_comboBox = (NSComboBox*)w;
104 }
105
106 wxNSComboBoxControl::~wxNSComboBoxControl()
107 {
108 }
109
110 int wxNSComboBoxControl::GetSelectedItem() const
111 {
112 return [m_comboBox indexOfSelectedItem];
113 }
114
115 void wxNSComboBoxControl::SetSelectedItem(int item)
116 {
117 SendEvents(false);
118
119 if ( item != wxNOT_FOUND )
120 {
121 wxASSERT_MSG( item >= 0 && item < [m_comboBox numberOfItems],
122 "Inavlid item index." );
123 [m_comboBox selectItemAtIndex: item];
124 }
125 else // remove current selection (if we have any)
126 {
127 const int sel = GetSelectedItem();
128 if ( sel != wxNOT_FOUND )
129 [m_comboBox deselectItemAtIndex:sel];
130 }
131
132 SendEvents(true);
133 }
134
135 int wxNSComboBoxControl::GetNumberOfItems() const
136 {
137 return [m_comboBox numberOfItems];
138 }
139
140 void wxNSComboBoxControl::InsertItem(int pos, const wxString& item)
141 {
142 [m_comboBox insertItemWithObjectValue:wxCFStringRef( item , m_wxPeer->GetFont().GetEncoding() ).AsNSString() atIndex:pos];
143 }
144
145 void wxNSComboBoxControl::RemoveItem(int pos)
146 {
147 SendEvents(false);
148 [m_comboBox removeItemAtIndex:pos];
149 SendEvents(true);
150 }
151
152 void wxNSComboBoxControl::Clear()
153 {
154 SendEvents(false);
155 [m_comboBox removeAllItems];
156 SendEvents(true);
157 }
158
159 wxString wxNSComboBoxControl::GetStringAtIndex(int pos) const
160 {
161 return wxCFStringRef::AsString([m_comboBox itemObjectValueAtIndex:pos], m_wxPeer->GetFont().GetEncoding());
162 }
163
164 int wxNSComboBoxControl::FindString(const wxString& text) const
165 {
166 NSInteger nsresult = [m_comboBox indexOfItemWithObjectValue:wxCFStringRef( text , m_wxPeer->GetFont().GetEncoding() ).AsNSString()];
167
168 int result;
169 if (nsresult == NSNotFound)
170 result = wxNOT_FOUND;
171 else
172 result = (int) nsresult;
173 return result;
174 }
175
176 void wxNSComboBoxControl::Popup()
177 {
178 id ax = NSAccessibilityUnignoredDescendant(m_comboBox);
179 [ax accessibilitySetValue: [NSNumber numberWithBool: YES] forAttribute: NSAccessibilityExpandedAttribute];
180 }
181
182 void wxNSComboBoxControl::Dismiss()
183 {
184 id ax = NSAccessibilityUnignoredDescendant(m_comboBox);
185 [ax accessibilitySetValue: [NSNumber numberWithBool: NO] forAttribute: NSAccessibilityExpandedAttribute];
186 }
187
188 wxWidgetImplType* wxWidgetImpl::CreateComboBox( wxComboBox* wxpeer,
189 wxWindowMac* WXUNUSED(parent),
190 wxWindowID WXUNUSED(id),
191 wxMenu* WXUNUSED(menu),
192 const wxPoint& pos,
193 const wxSize& size,
194 long style,
195 long WXUNUSED(extraStyle))
196 {
197 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
198 wxNSComboBox* v = [[wxNSComboBox alloc] initWithFrame:r];
199 if (style & wxCB_READONLY)
200 [v setEditable:NO];
201 wxNSComboBoxControl* c = new wxNSComboBoxControl( wxpeer, v );
202 return c;
203 }
204
205 wxSize wxComboBox::DoGetBestSize() const
206 {
207 int lbWidth = GetCount() > 0 ? 20 : 100; // some defaults
208 wxSize baseSize = wxWindow::DoGetBestSize();
209 int lbHeight = baseSize.y;
210 int wLine;
211
212 {
213 wxClientDC dc(const_cast<wxComboBox*>(this));
214
215 // Find the widest line
216 for(unsigned int i = 0; i < GetCount(); i++)
217 {
218 wxString str(GetString(i));
219
220 wxCoord width, height ;
221 dc.GetTextExtent( str , &width, &height);
222 wLine = width ;
223
224 lbWidth = wxMax( lbWidth, wLine ) ;
225 }
226
227 // Add room for the popup arrow
228 lbWidth += 2 * lbHeight ;
229 }
230
231 return wxSize( lbWidth, lbHeight );
232 }
233
234 #endif // wxUSE_COMBOBOX