]>
Commit | Line | Data |
---|---|---|
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 | 78 | { |
2de58153 | 79 | wxComboBox* wxpeer = static_cast<wxComboBox*>(impl->GetWXPeer()); |
c84030e0 | 80 | if ( wxpeer ) { |
2de58153 VZ |
81 | const int sel = wxpeer->GetSelection(); |
82 | ||
c84030e0 KO |
83 | wxCommandEvent event(wxEVT_COMMAND_COMBOBOX_SELECTED, wxpeer->GetId()); |
84 | event.SetEventObject( wxpeer ); | |
2de58153 VZ |
85 | event.SetInt( sel ); |
86 | event.SetString( wxpeer->GetString(sel) ); | |
c84030e0 KO |
87 | // For some reason, wxComboBox::GetValue will not return the newly selected item |
88 | // while we're inside this callback, so use AddPendingEvent to make sure | |
89 | // GetValue() returns the right value. | |
18a7376a SC |
90 | wxEventLoop* const loop = (wxEventLoop*) wxEventLoopBase::GetActive(); |
91 | if ( loop ) | |
92 | loop->OSXUseLowLevelWakeup(true); | |
93 | ||
c84030e0 | 94 | wxpeer->GetEventHandler()->AddPendingEvent( event ); |
18a7376a SC |
95 | |
96 | if ( loop ) | |
97 | loop->OSXUseLowLevelWakeup(false); | |
c84030e0 KO |
98 | } |
99 | } | |
4ddfa282 | 100 | } |
4ddfa282 SC |
101 | @end |
102 | ||
c072b9ec VZ |
103 | wxNSComboBoxControl::wxNSComboBoxControl( wxComboBox *wxPeer, WXWidget w ) |
104 | : wxNSTextFieldControl(wxPeer, wxPeer, w) | |
c84030e0 KO |
105 | { |
106 | m_comboBox = (NSComboBox*)w; | |
107 | } | |
108 | ||
109 | wxNSComboBoxControl::~wxNSComboBoxControl() | |
110 | { | |
111 | } | |
112 | ||
113 | int wxNSComboBoxControl::GetSelectedItem() const | |
114 | { | |
115 | return [m_comboBox indexOfSelectedItem]; | |
116 | } | |
117 | ||
118 | void wxNSComboBoxControl::SetSelectedItem(int item) | |
119 | { | |
809020fc | 120 | SendEvents(false); |
6f07c007 VZ |
121 | |
122 | if ( item != wxNOT_FOUND ) | |
123 | { | |
124 | wxASSERT_MSG( item >= 0 && item < [m_comboBox numberOfItems], | |
125 | "Inavlid item index." ); | |
126 | [m_comboBox selectItemAtIndex: item]; | |
127 | } | |
128 | else // remove current selection (if we have any) | |
129 | { | |
130 | const int sel = GetSelectedItem(); | |
131 | if ( sel != wxNOT_FOUND ) | |
132 | [m_comboBox deselectItemAtIndex:sel]; | |
133 | } | |
134 | ||
809020fc | 135 | SendEvents(true); |
c84030e0 KO |
136 | } |
137 | ||
138 | int wxNSComboBoxControl::GetNumberOfItems() const | |
139 | { | |
140 | return [m_comboBox numberOfItems]; | |
141 | } | |
142 | ||
143 | void wxNSComboBoxControl::InsertItem(int pos, const wxString& item) | |
144 | { | |
145 | [m_comboBox insertItemWithObjectValue:wxCFStringRef( item , m_wxPeer->GetFont().GetEncoding() ).AsNSString() atIndex:pos]; | |
146 | } | |
147 | ||
148 | void wxNSComboBoxControl::RemoveItem(int pos) | |
149 | { | |
809020fc | 150 | SendEvents(false); |
c84030e0 | 151 | [m_comboBox removeItemAtIndex:pos]; |
809020fc | 152 | SendEvents(true); |
c84030e0 KO |
153 | } |
154 | ||
155 | void wxNSComboBoxControl::Clear() | |
156 | { | |
809020fc | 157 | SendEvents(false); |
c84030e0 | 158 | [m_comboBox removeAllItems]; |
809020fc | 159 | SendEvents(true); |
c84030e0 KO |
160 | } |
161 | ||
162 | wxString wxNSComboBoxControl::GetStringAtIndex(int pos) const | |
163 | { | |
164 | return wxCFStringRef::AsString([m_comboBox itemObjectValueAtIndex:pos], m_wxPeer->GetFont().GetEncoding()); | |
165 | } | |
166 | ||
167 | int wxNSComboBoxControl::FindString(const wxString& text) const | |
168 | { | |
79323592 SC |
169 | NSInteger nsresult = [m_comboBox indexOfItemWithObjectValue:wxCFStringRef( text , m_wxPeer->GetFont().GetEncoding() ).AsNSString()]; |
170 | ||
171 | int result; | |
172 | if (nsresult == NSNotFound) | |
2c755d9b | 173 | result = wxNOT_FOUND; |
79323592 SC |
174 | else |
175 | result = (int) nsresult; | |
2c755d9b | 176 | return result; |
c84030e0 KO |
177 | } |
178 | ||
ff8cb900 VZ |
179 | void wxNSComboBoxControl::Popup() |
180 | { | |
181 | id ax = NSAccessibilityUnignoredDescendant(m_comboBox); | |
182 | [ax accessibilitySetValue: [NSNumber numberWithBool: YES] forAttribute: NSAccessibilityExpandedAttribute]; | |
183 | } | |
184 | ||
185 | void wxNSComboBoxControl::Dismiss() | |
186 | { | |
187 | id ax = NSAccessibilityUnignoredDescendant(m_comboBox); | |
188 | [ax accessibilitySetValue: [NSNumber numberWithBool: NO] forAttribute: NSAccessibilityExpandedAttribute]; | |
189 | } | |
190 | ||
c072b9ec | 191 | wxWidgetImplType* wxWidgetImpl::CreateComboBox( wxComboBox* wxpeer, |
4ddfa282 SC |
192 | wxWindowMac* WXUNUSED(parent), |
193 | wxWindowID WXUNUSED(id), | |
e7794cf2 | 194 | wxMenu* WXUNUSED(menu), |
4ddfa282 SC |
195 | const wxPoint& pos, |
196 | const wxSize& size, | |
ec073e73 | 197 | long style, |
4ddfa282 SC |
198 | long WXUNUSED(extraStyle)) |
199 | { | |
200 | NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ; | |
f941a30b | 201 | wxNSComboBox* v = [[wxNSComboBox alloc] initWithFrame:r]; |
ec073e73 KO |
202 | if (style & wxCB_READONLY) |
203 | [v setEditable:NO]; | |
c84030e0 | 204 | wxNSComboBoxControl* c = new wxNSComboBoxControl( wxpeer, v ); |
4ddfa282 SC |
205 | return c; |
206 | } | |
207 | ||
5bd77105 SC |
208 | wxSize wxComboBox::DoGetBestSize() const |
209 | { | |
210 | int lbWidth = GetCount() > 0 ? 20 : 100; // some defaults | |
211 | wxSize baseSize = wxWindow::DoGetBestSize(); | |
212 | int lbHeight = baseSize.y; | |
213 | int wLine; | |
214 | ||
215 | { | |
216 | wxClientDC dc(const_cast<wxComboBox*>(this)); | |
217 | ||
218 | // Find the widest line | |
219 | for(unsigned int i = 0; i < GetCount(); i++) | |
220 | { | |
221 | wxString str(GetString(i)); | |
222 | ||
223 | wxCoord width, height ; | |
224 | dc.GetTextExtent( str , &width, &height); | |
225 | wLine = width ; | |
226 | ||
227 | lbWidth = wxMax( lbWidth, wLine ) ; | |
228 | } | |
229 | ||
230 | // Add room for the popup arrow | |
231 | lbWidth += 2 * lbHeight ; | |
232 | } | |
233 | ||
234 | return wxSize( lbWidth, lbHeight ); | |
235 | } | |
236 | ||
c072b9ec | 237 | #endif // wxUSE_COMBOBOX |