]> git.saurik.com Git - wxWidgets.git/blame - src/osx/cocoa/combobox.mm
Improve check for OS X version in Cocoa wxFileDialog implementation.
[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@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
c84030e0 57- (void)controlTextDidChange:(NSNotification *)aNotification
4ddfa282 58{
c84030e0
KO
59 wxUnusedVar(aNotification);
60 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
809020fc 61 if ( impl && impl->ShouldSendEvents() )
c84030e0
KO
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 }
4ddfa282
SC
71}
72
c84030e0 73- (void)comboBoxSelectionDidChange:(NSNotification *)notification
4ddfa282 74{
c84030e0
KO
75 wxUnusedVar(notification);
76 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
809020fc 77 if ( impl && impl->ShouldSendEvents())
c84030e0
KO
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.
18a7376a
SC
87 wxEventLoop* const loop = (wxEventLoop*) wxEventLoopBase::GetActive();
88 if ( loop )
89 loop->OSXUseLowLevelWakeup(true);
90
c84030e0 91 wxpeer->GetEventHandler()->AddPendingEvent( event );
18a7376a
SC
92
93 if ( loop )
94 loop->OSXUseLowLevelWakeup(false);
c84030e0
KO
95 }
96 }
4ddfa282 97}
4ddfa282
SC
98@end
99
c072b9ec
VZ
100wxNSComboBoxControl::wxNSComboBoxControl( wxComboBox *wxPeer, WXWidget w )
101 : wxNSTextFieldControl(wxPeer, wxPeer, w)
c84030e0
KO
102{
103 m_comboBox = (NSComboBox*)w;
104}
105
106wxNSComboBoxControl::~wxNSComboBoxControl()
107{
108}
109
110int wxNSComboBoxControl::GetSelectedItem() const
111{
112 return [m_comboBox indexOfSelectedItem];
113}
114
115void wxNSComboBoxControl::SetSelectedItem(int item)
116{
809020fc 117 SendEvents(false);
6f07c007
VZ
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
809020fc 132 SendEvents(true);
c84030e0
KO
133}
134
135int wxNSComboBoxControl::GetNumberOfItems() const
136{
137 return [m_comboBox numberOfItems];
138}
139
140void wxNSComboBoxControl::InsertItem(int pos, const wxString& item)
141{
142 [m_comboBox insertItemWithObjectValue:wxCFStringRef( item , m_wxPeer->GetFont().GetEncoding() ).AsNSString() atIndex:pos];
143}
144
145void wxNSComboBoxControl::RemoveItem(int pos)
146{
809020fc 147 SendEvents(false);
c84030e0 148 [m_comboBox removeItemAtIndex:pos];
809020fc 149 SendEvents(true);
c84030e0
KO
150}
151
152void wxNSComboBoxControl::Clear()
153{
809020fc 154 SendEvents(false);
c84030e0 155 [m_comboBox removeAllItems];
809020fc 156 SendEvents(true);
c84030e0
KO
157}
158
159wxString wxNSComboBoxControl::GetStringAtIndex(int pos) const
160{
161 return wxCFStringRef::AsString([m_comboBox itemObjectValueAtIndex:pos], m_wxPeer->GetFont().GetEncoding());
162}
163
164int wxNSComboBoxControl::FindString(const wxString& text) const
165{
79323592
SC
166 NSInteger nsresult = [m_comboBox indexOfItemWithObjectValue:wxCFStringRef( text , m_wxPeer->GetFont().GetEncoding() ).AsNSString()];
167
168 int result;
169 if (nsresult == NSNotFound)
2c755d9b 170 result = wxNOT_FOUND;
79323592
SC
171 else
172 result = (int) nsresult;
2c755d9b 173 return result;
c84030e0
KO
174}
175
ff8cb900
VZ
176void wxNSComboBoxControl::Popup()
177{
178 id ax = NSAccessibilityUnignoredDescendant(m_comboBox);
179 [ax accessibilitySetValue: [NSNumber numberWithBool: YES] forAttribute: NSAccessibilityExpandedAttribute];
180}
181
182void wxNSComboBoxControl::Dismiss()
183{
184 id ax = NSAccessibilityUnignoredDescendant(m_comboBox);
185 [ax accessibilitySetValue: [NSNumber numberWithBool: NO] forAttribute: NSAccessibilityExpandedAttribute];
186}
187
c072b9ec 188wxWidgetImplType* wxWidgetImpl::CreateComboBox( wxComboBox* wxpeer,
4ddfa282
SC
189 wxWindowMac* WXUNUSED(parent),
190 wxWindowID WXUNUSED(id),
e7794cf2 191 wxMenu* WXUNUSED(menu),
4ddfa282
SC
192 const wxPoint& pos,
193 const wxSize& size,
ec073e73 194 long style,
4ddfa282
SC
195 long WXUNUSED(extraStyle))
196{
197 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
f941a30b 198 wxNSComboBox* v = [[wxNSComboBox alloc] initWithFrame:r];
ec073e73
KO
199 if (style & wxCB_READONLY)
200 [v setEditable:NO];
c84030e0 201 wxNSComboBoxControl* c = new wxNSComboBoxControl( wxpeer, v );
4ddfa282
SC
202 return c;
203}
204
5bd77105
SC
205wxSize 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
c072b9ec 234#endif // wxUSE_COMBOBOX