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