]> git.saurik.com Git - wxWidgets.git/blame - src/osx/cocoa/combobox.mm
Add wxTimer::StartOnce().
[wxWidgets.git] / src / osx / cocoa / combobox.mm
CommitLineData
4ddfa282
SC
1/////////////////////////////////////////////////////////////////////////////
2// Name: src/osx/cocoa/combobox.mm
3// Purpose: wxChoice
4// Author: Stefan Csomor
5// Modified by:
6// Created: 1998-01-01
a9a4f229 7// RCS-ID: $Id$
4ddfa282
SC
8// Copyright: (c) Stefan Csomor
9// Licence: wxWindows licence
10/////////////////////////////////////////////////////////////////////////////
11
12#include "wx/wxprec.h"
13
c84030e0 14#if wxUSE_COMBOBOX
4ddfa282
SC
15
16#include "wx/combobox.h"
18a7376a 17#include "wx/evtloop.h"
4ddfa282
SC
18
19#ifndef WX_PRECOMP
20 #include "wx/menu.h"
21 #include "wx/dcclient.h"
22#endif
23
c84030e0 24#include "wx/osx/cocoa/private/textimpl.h"
4ddfa282
SC
25
26// work in progress
27
28953245
SC
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
4ddfa282
SC
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
d9307d00
SC
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
c84030e0 78- (void)controlTextDidChange:(NSNotification *)aNotification
4ddfa282 79{
c84030e0
KO
80 wxUnusedVar(aNotification);
81 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
809020fc 82 if ( impl && impl->ShouldSendEvents() )
c84030e0
KO
83 {
84 wxWindow* wxpeer = (wxWindow*) impl->GetWXPeer();
85 if ( wxpeer ) {
ce7fe42e 86 wxCommandEvent event(wxEVT_TEXT, wxpeer->GetId());
c84030e0
KO
87 event.SetEventObject( wxpeer );
88 event.SetString( static_cast<wxComboBox*>(wxpeer)->GetValue() );
89 wxpeer->HandleWindowEvent( event );
90 }
91 }
4ddfa282
SC
92}
93
c84030e0 94- (void)comboBoxSelectionDidChange:(NSNotification *)notification
4ddfa282 95{
c84030e0
KO
96 wxUnusedVar(notification);
97 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
809020fc 98 if ( impl && impl->ShouldSendEvents())
c84030e0 99 {
2de58153 100 wxComboBox* wxpeer = static_cast<wxComboBox*>(impl->GetWXPeer());
c84030e0 101 if ( wxpeer ) {
2de58153
VZ
102 const int sel = wxpeer->GetSelection();
103
ce7fe42e 104 wxCommandEvent event(wxEVT_COMBOBOX, wxpeer->GetId());
c84030e0 105 event.SetEventObject( wxpeer );
2de58153
VZ
106 event.SetInt( sel );
107 event.SetString( wxpeer->GetString(sel) );
c84030e0
KO
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.
18a7376a 111
c84030e0 112 wxpeer->GetEventHandler()->AddPendingEvent( event );
18a7376a 113
c84030e0
KO
114 }
115 }
4ddfa282 116}
4ddfa282
SC
117@end
118
c072b9ec
VZ
119wxNSComboBoxControl::wxNSComboBoxControl( wxComboBox *wxPeer, WXWidget w )
120 : wxNSTextFieldControl(wxPeer, wxPeer, w)
c84030e0
KO
121{
122 m_comboBox = (NSComboBox*)w;
123}
124
125wxNSComboBoxControl::~wxNSComboBoxControl()
126{
127}
128
06e56e62
SC
129void wxNSComboBoxControl::mouseEvent(WX_NSEvent event, WXWidget slf, void *_cmd)
130{
131 // NSComboBox has its own event loop, which reacts very badly to our synthetic
132 // events used to signal when a wxEvent is posted, so during that time we switch
133 // the wxEventLoop::WakeUp implementation to a lower-level version
134
135 bool reset = false;
136 wxEventLoop* const loop = (wxEventLoop*) wxEventLoopBase::GetActive();
137
138 if ( loop != NULL && [event type] == NSLeftMouseDown )
139 {
140 reset = true;
141 loop->OSXUseLowLevelWakeup(true);
142 }
143
144 wxOSX_EventHandlerPtr superimpl = (wxOSX_EventHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
145 superimpl(slf, (SEL)_cmd, event);
146
147 if ( reset )
148 {
5928f660 149 loop->OSXUseLowLevelWakeup(false);
06e56e62
SC
150 }
151}
152
c84030e0
KO
153int wxNSComboBoxControl::GetSelectedItem() const
154{
155 return [m_comboBox indexOfSelectedItem];
156}
157
158void wxNSComboBoxControl::SetSelectedItem(int item)
159{
809020fc 160 SendEvents(false);
6f07c007
VZ
161
162 if ( item != wxNOT_FOUND )
163 {
164 wxASSERT_MSG( item >= 0 && item < [m_comboBox numberOfItems],
165 "Inavlid item index." );
166 [m_comboBox selectItemAtIndex: item];
167 }
168 else // remove current selection (if we have any)
169 {
170 const int sel = GetSelectedItem();
171 if ( sel != wxNOT_FOUND )
172 [m_comboBox deselectItemAtIndex:sel];
173 }
174
809020fc 175 SendEvents(true);
c84030e0
KO
176}
177
178int wxNSComboBoxControl::GetNumberOfItems() const
179{
180 return [m_comboBox numberOfItems];
181}
182
183void wxNSComboBoxControl::InsertItem(int pos, const wxString& item)
184{
185 [m_comboBox insertItemWithObjectValue:wxCFStringRef( item , m_wxPeer->GetFont().GetEncoding() ).AsNSString() atIndex:pos];
186}
187
188void wxNSComboBoxControl::RemoveItem(int pos)
189{
809020fc 190 SendEvents(false);
c84030e0 191 [m_comboBox removeItemAtIndex:pos];
809020fc 192 SendEvents(true);
c84030e0
KO
193}
194
195void wxNSComboBoxControl::Clear()
196{
809020fc 197 SendEvents(false);
c84030e0 198 [m_comboBox removeAllItems];
1d90958b 199 [m_comboBox setStringValue:@""];
809020fc 200 SendEvents(true);
c84030e0
KO
201}
202
203wxString wxNSComboBoxControl::GetStringAtIndex(int pos) const
204{
205 return wxCFStringRef::AsString([m_comboBox itemObjectValueAtIndex:pos], m_wxPeer->GetFont().GetEncoding());
206}
207
208int wxNSComboBoxControl::FindString(const wxString& text) const
209{
79323592
SC
210 NSInteger nsresult = [m_comboBox indexOfItemWithObjectValue:wxCFStringRef( text , m_wxPeer->GetFont().GetEncoding() ).AsNSString()];
211
212 int result;
213 if (nsresult == NSNotFound)
2c755d9b 214 result = wxNOT_FOUND;
79323592
SC
215 else
216 result = (int) nsresult;
2c755d9b 217 return result;
c84030e0
KO
218}
219
ff8cb900
VZ
220void wxNSComboBoxControl::Popup()
221{
222 id ax = NSAccessibilityUnignoredDescendant(m_comboBox);
223 [ax accessibilitySetValue: [NSNumber numberWithBool: YES] forAttribute: NSAccessibilityExpandedAttribute];
224}
225
226void wxNSComboBoxControl::Dismiss()
227{
228 id ax = NSAccessibilityUnignoredDescendant(m_comboBox);
229 [ax accessibilitySetValue: [NSNumber numberWithBool: NO] forAttribute: NSAccessibilityExpandedAttribute];
230}
231
d4e5c5b9
SC
232void wxNSComboBoxControl::SetEditable(bool editable)
233{
234 // TODO: unfortunately this does not work, setEditable just means the same as CB_READONLY
235 // I don't see a way to access the text field directly
06e56e62
SC
236
237 // Behavior NONE <- SELECTECTABLE
238 [m_comboBox setEditable:editable];
d4e5c5b9
SC
239}
240
c072b9ec 241wxWidgetImplType* wxWidgetImpl::CreateComboBox( wxComboBox* wxpeer,
4ddfa282
SC
242 wxWindowMac* WXUNUSED(parent),
243 wxWindowID WXUNUSED(id),
e7794cf2 244 wxMenu* WXUNUSED(menu),
4ddfa282
SC
245 const wxPoint& pos,
246 const wxSize& size,
ec073e73 247 long style,
4ddfa282
SC
248 long WXUNUSED(extraStyle))
249{
250 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
f941a30b 251 wxNSComboBox* v = [[wxNSComboBox alloc] initWithFrame:r];
ec073e73
KO
252 if (style & wxCB_READONLY)
253 [v setEditable:NO];
c84030e0 254 wxNSComboBoxControl* c = new wxNSComboBoxControl( wxpeer, v );
4ddfa282
SC
255 return c;
256}
257
5bd77105
SC
258wxSize wxComboBox::DoGetBestSize() const
259{
260 int lbWidth = GetCount() > 0 ? 20 : 100; // some defaults
261 wxSize baseSize = wxWindow::DoGetBestSize();
262 int lbHeight = baseSize.y;
263 int wLine;
264
265 {
266 wxClientDC dc(const_cast<wxComboBox*>(this));
267
268 // Find the widest line
269 for(unsigned int i = 0; i < GetCount(); i++)
270 {
271 wxString str(GetString(i));
272
273 wxCoord width, height ;
274 dc.GetTextExtent( str , &width, &height);
275 wLine = width ;
276
277 lbWidth = wxMax( lbWidth, wLine ) ;
278 }
279
280 // Add room for the popup arrow
281 lbWidth += 2 * lbHeight ;
282 }
283
284 return wxSize( lbWidth, lbHeight );
285}
286
c072b9ec 287#endif // wxUSE_COMBOBOX