]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: src/cocoa/combobox.mm | |
3 | // Purpose: wxComboBox | |
4 | // Author: Ryan Norton | |
5 | // Modified by: | |
6 | // Created: 2005/02/16 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) 2003 David Elliott | |
9 | // Licence: wxWidgets licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | // | |
13 | // Impl notes: | |
14 | // There is no custom data source because doing so unnecessarily sacrifices | |
15 | // some native autocompletion behavior (we would have to make our own - | |
16 | // the SimpleComboBox sample does so in the developer folder that | |
17 | // comes with OSX). One reason you might want this would be to have | |
18 | // only one array or be able to display numbers returned by an NSNumber | |
19 | // from the methods. | |
20 | // | |
21 | // One problem though is that wxCB_SORT isn't implemented... | |
22 | // | |
23 | // Also, not sure if it is correctly getting text field events since | |
24 | // I'm using SetNSComboBox instead of SetNSTextField | |
25 | // | |
26 | // doWxEvent is really hackish... but since there's only one event... | |
27 | // | |
28 | // Ideas for future improvement - other notes: | |
29 | // Combox w/o wxCB_DROPDOWN doesn't seem to be implementable | |
30 | //wxCB_READONLY Same as wxCB_DROPDOWN but only the strings specified as the combobox choices can be selected, it is impossible to select (even from a program) a string which is not in the choices list. | |
31 | //wxCB_SORT is possible with data source | |
32 | // | |
33 | // setIntercellSpacing:/setItemHeight: to autoadjust to number of inserted items? | |
34 | // | |
35 | /* | |
36 | //example of autocompletion from SimpleComboBox Example | |
37 | // ========================================================== | |
38 | // Combo box data source methods | |
39 | // ========================================================== | |
40 | ||
41 | - (int)numberOfItemsInComboBox:(NSComboBox *)aComboBox { | |
42 | return [genres count]; | |
43 | } | |
44 | - (id)comboBox:(NSComboBox *)aComboBox objectValueForItemAtIndex:(int)index { | |
45 | return [genres objectAtIndex:index]; | |
46 | } | |
47 | - (unsigned int)comboBox:(NSComboBox *)aComboBox indexOfItemWithStringValue:(NSString *)string { | |
48 | return [genres indexOfObject: string]; | |
49 | } | |
50 | ||
51 | - (NSString *) firstGenreMatchingPrefix:(NSString *)prefix { | |
52 | NSString *string = nil; | |
53 | NSString *lowercasePrefix = [prefix lowercaseString]; | |
54 | NSEnumerator *stringEnum = [genres objectEnumerator]; | |
55 | while ((string = [stringEnum nextObject])) { | |
56 | if ([[string lowercaseString] hasPrefix: lowercasePrefix]) return string; | |
57 | } | |
58 | return nil; | |
59 | } | |
60 | ||
61 | - (NSString *)comboBox:(NSComboBox *)aComboBox completedString:(NSString *)inputString { | |
62 | // This method is received after each character typed by the user, because we have checked the "completes" flag for genreComboBox in IB. | |
63 | // Given the inputString the user has typed, see if we can find a genre with the prefix, and return it as the suggested complete string. | |
64 | NSString *candidate = [self firstGenreMatchingPrefix: inputString]; | |
65 | return (candidate ? candidate : inputString); | |
66 | } | |
67 | */ | |
68 | ||
69 | // ============================================================================ | |
70 | // declarations | |
71 | // ============================================================================ | |
72 | ||
73 | // ---------------------------------------------------------------------------- | |
74 | // headers | |
75 | // ---------------------------------------------------------------------------- | |
76 | ||
77 | #include "wx/wxprec.h" | |
78 | ||
79 | #if wxUSE_COMBOBOX | |
80 | ||
81 | #ifndef WX_PRECOMP | |
82 | #include "wx/window.h" | |
83 | #endif // WX_PRECOMP | |
84 | ||
85 | #include "wx/cocoa/ObjcPose.h" | |
86 | #include "wx/combobox.h" | |
87 | ||
88 | #import <AppKit/NSComboBox.h> | |
89 | #import <Foundation/NSNotification.h> | |
90 | #import <Foundation/NSString.h> | |
91 | ||
92 | // ---------------------------------------------------------------------------- | |
93 | // globals | |
94 | // ---------------------------------------------------------------------------- | |
95 | WX_IMPLEMENT_OBJC_INTERFACE_HASHMAP(NSComboBox) | |
96 | ||
97 | void wxCocoaNSComboBox::AssociateNSComboBox(WX_NSComboBox cocoaNSComboBox) | |
98 | { | |
99 | if(cocoaNSComboBox) | |
100 | { | |
101 | sm_cocoaHash.insert(wxCocoaNSComboBoxHash::value_type(cocoaNSComboBox,this)); | |
102 | ||
103 | [[NSNotificationCenter defaultCenter] addObserver:(id)cocoaNSComboBox selector:@selector(comboBoxSelectionDidChange:) name:@"NSComboBoxSelectionDidChangeNotification" object:cocoaNSComboBox]; | |
104 | [[NSNotificationCenter defaultCenter] addObserver:(id)cocoaNSComboBox selector:@selector(comboBoxSelectionDidChange:) name:@"NSComboBoxSelectionIsChangingNotification" object:cocoaNSComboBox]; | |
105 | [[NSNotificationCenter defaultCenter] addObserver:(id)cocoaNSComboBox selector:@selector(comboBoxSelectionDidChange:) name:@"NSComboBoxWillDismissNotification" object:cocoaNSComboBox]; | |
106 | [[NSNotificationCenter defaultCenter] addObserver:(id)cocoaNSComboBox selector:@selector(comboBoxSelectionDidChange:) name:@"NSComboBoxWillPopUpNotification" object:cocoaNSComboBox]; | |
107 | } | |
108 | } | |
109 | ||
110 | void wxCocoaNSComboBox::DisassociateNSComboBox(WX_NSComboBox cocoaNSComboBox) | |
111 | { | |
112 | if(cocoaNSComboBox) | |
113 | { | |
114 | sm_cocoaHash.erase(cocoaNSComboBox); | |
115 | [[NSNotificationCenter defaultCenter] removeObserver:(id)cocoaNSComboBox name:@"NSComboBoxSelectionDidChangeNotification" object:cocoaNSComboBox]; | |
116 | [[NSNotificationCenter defaultCenter] removeObserver:(id)cocoaNSComboBox name:@"NSComboBoxSelectionIsChangingNotification" object:cocoaNSComboBox]; | |
117 | [[NSNotificationCenter defaultCenter] removeObserver:(id)cocoaNSComboBox name:@"NSComboBoxWillDismissNotification" object:cocoaNSComboBox]; | |
118 | [[NSNotificationCenter defaultCenter] removeObserver:(id)cocoaNSComboBox name:@"NSComboBoxWillPopUpNotification" object:cocoaNSComboBox]; | |
119 | } | |
120 | } | |
121 | ||
122 | // ============================================================================ | |
123 | // @class wxPoserNSComboBox | |
124 | // ============================================================================ | |
125 | @interface wxPoserNSComboBox : NSComboBox | |
126 | { | |
127 | } | |
128 | ||
129 | - (void)comboBoxSelectionDidChange:(NSNotification *)notification; | |
130 | - (void)comboBoxSelectionIsChanging:(NSNotification *)notification; | |
131 | - (void)comboBoxWillDismiss:(NSNotification *)notification; | |
132 | - (void)comboBoxWillPopUp:(NSNotification *)notification; | |
133 | @end // wxPoserNSComboBox | |
134 | ||
135 | //WX_IMPLEMENT_POSER(wxPoserNSComboBox); | |
136 | @implementation wxPoserNSComboBox : NSComboBox | |
137 | ||
138 | - (void)comboBoxSelectionDidChange:(NSNotification *)notification | |
139 | { | |
140 | wxCocoaNSComboBox *win = wxCocoaNSComboBox::GetFromCocoa(self); | |
141 | win->doWxEvent(wxEVT_COMMAND_COMBOBOX_SELECTED); | |
142 | } | |
143 | ||
144 | - (void)comboBoxSelectionIsChanging:(NSNotification *)notification | |
145 | { | |
146 | //... | |
147 | } | |
148 | ||
149 | - (void)comboBoxWillDismiss:(NSNotification *)notification | |
150 | { | |
151 | //... | |
152 | } | |
153 | ||
154 | - (void)comboBoxWillPopUp:(NSNotification *)notification | |
155 | { | |
156 | //... | |
157 | } | |
158 | ||
159 | @end // implementation wxPoserNSComboBox | |
160 | ||
161 | #include "wx/app.h" | |
162 | #include "wx/combobox.h" | |
163 | #include "wx/log.h" | |
164 | ||
165 | #include "wx/cocoa/autorelease.h" | |
166 | #include "wx/cocoa/string.h" | |
167 | ||
168 | #import <AppKit/NSComboBox.h> | |
169 | ||
170 | IMPLEMENT_DYNAMIC_CLASS(wxComboBox, wxTextCtrl) | |
171 | BEGIN_EVENT_TABLE(wxComboBox, wxTextCtrl) | |
172 | END_EVENT_TABLE() | |
173 | WX_IMPLEMENT_COCOA_OWNER(wxComboBox,NSComboBox,NSTextField,NSView) | |
174 | ||
175 | bool wxComboBox::Create(wxWindow *parent, wxWindowID winid, | |
176 | const wxString& value, | |
177 | const wxPoint& pos, | |
178 | const wxSize& size, | |
179 | const wxArrayString& choices, | |
180 | long style, | |
181 | const wxValidator& validator, | |
182 | const wxString& name) | |
183 | { | |
184 | wxCArrayString chs(choices); | |
185 | ||
186 | return Create(parent, winid, value, pos, size, chs.GetCount(), | |
187 | chs.GetStrings(), style, validator, name); | |
188 | } | |
189 | ||
190 | bool wxComboBox::Create(wxWindow *parent, wxWindowID winid, | |
191 | const wxString& value, | |
192 | const wxPoint& pos, | |
193 | const wxSize& size, | |
194 | int n, const wxString choices[], | |
195 | long style, | |
196 | const wxValidator& validator, | |
197 | const wxString& name) | |
198 | { | |
199 | wxAutoNSAutoreleasePool pool; | |
200 | if(!CreateControl(parent,winid,pos,size,style,validator,name)) | |
201 | return false; | |
202 | ||
203 | m_cocoaNSView = NULL; | |
204 | SetNSComboBox([[wxPoserNSComboBox alloc] initWithFrame:MakeDefaultNSRect(size)]); | |
205 | [m_cocoaNSView release]; | |
206 | [GetNSTextField() setStringValue:wxNSStringWithWxString(value.c_str())]; | |
207 | [GetNSControl() sizeToFit]; | |
208 | if(m_parent) | |
209 | m_parent->CocoaAddChild(this); | |
210 | SetInitialFrameRect(pos,size); | |
211 | ||
212 | for(int i = 0; i < n; ++i) | |
213 | wxComboBox::DoAppend(choices[i]); | |
214 | ||
215 | [GetNSComboBox() setCompletes:true]; //autocomplete :) | |
216 | ||
217 | return true; | |
218 | } | |
219 | ||
220 | wxComboBox::~wxComboBox() | |
221 | { | |
222 | DisassociateNSComboBox(GetNSComboBox()); | |
223 | } | |
224 | ||
225 | void wxComboBox::doWxEvent(int nEvent) | |
226 | { | |
227 | wxCommandEvent event2(wxEVT_COMMAND_COMBOBOX_SELECTED, GetId() ); | |
228 | event2.SetInt(GetSelection()); | |
229 | event2.SetEventObject(this); | |
230 | event2.SetString(GetStringSelection()); | |
231 | GetEventHandler()->ProcessEvent(event2); | |
232 | ||
233 | // For consistency with MSW and GTK, also send a text updated event | |
234 | // After all, the text is updated when a selection is made | |
235 | wxCommandEvent TextEvent( wxEVT_COMMAND_TEXT_UPDATED, GetId() ); | |
236 | TextEvent.SetString( GetStringSelection() ); | |
237 | TextEvent.SetEventObject( this ); | |
238 | GetEventHandler()->ProcessEvent( TextEvent ); | |
239 | } | |
240 | ||
241 | ||
242 | void wxComboBox::SetSelection(int nSelection) | |
243 | { | |
244 | [GetNSComboBox() selectItemAtIndex:nSelection]; | |
245 | } | |
246 | ||
247 | wxString wxComboBox::GetStringSelection() | |
248 | { | |
249 | return wxStringWithNSString([GetNSComboBox() objectValueOfSelectedItem]); | |
250 | } | |
251 | ||
252 | void wxComboBox::Clear() | |
253 | { | |
254 | [GetNSComboBox() removeAllItems]; | |
255 | m_Datas.Clear(); | |
256 | } | |
257 | ||
258 | void wxComboBox::Delete(int nIndex) | |
259 | { | |
260 | [GetNSComboBox() removeItemAtIndex:nIndex]; | |
261 | m_Datas.RemoveAt(nIndex); | |
262 | } | |
263 | ||
264 | size_t wxComboBox::GetCount() const | |
265 | { | |
266 | return (size_t)[GetNSComboBox() numberOfItems]; | |
267 | } | |
268 | ||
269 | wxString wxComboBox::GetString(int nIndex) const | |
270 | { | |
271 | return wxStringWithNSString([GetNSComboBox() itemObjectValueAtIndex:nIndex]); | |
272 | } | |
273 | ||
274 | void wxComboBox::SetString(int nIndex, const wxString& szString) | |
275 | { | |
276 | wxAutoNSAutoreleasePool pool; | |
277 | //FIXME: There appears to be no "set item data" method - maybe | |
278 | //an assignment would work? | |
279 | [GetNSComboBox() removeItemAtIndex:nIndex]; | |
280 | [GetNSComboBox() insertItemWithObjectValue:wxNSStringWithWxString(szString) atIndex:nIndex]; | |
281 | } | |
282 | ||
283 | int wxComboBox::FindString(const wxString& szItem, bool bCase) const | |
284 | { | |
285 | // FIXME: use wxItemContainerImmutable::FindString for bCase parameter | |
286 | return [GetNSComboBox() indexOfItemWithObjectValue:wxNSStringWithWxString(szItem)]; | |
287 | } | |
288 | ||
289 | int wxComboBox::GetSelection() const | |
290 | { | |
291 | return [GetNSComboBox() indexOfSelectedItem]; | |
292 | } | |
293 | ||
294 | int wxComboBox::DoAppend(const wxString& szItem) | |
295 | { | |
296 | m_Datas.Add(NULL); | |
297 | wxAutoNSAutoreleasePool pool; | |
298 | [GetNSComboBox() addItemWithObjectValue:wxNSStringWithWxString(szItem)]; | |
299 | return [GetNSComboBox() numberOfItems]; | |
300 | } | |
301 | ||
302 | int wxComboBox::DoInsert(const wxString& szItem, int nIndex) | |
303 | { | |
304 | m_Datas.Insert(NULL, nIndex); | |
305 | wxAutoNSAutoreleasePool pool; | |
306 | [GetNSComboBox() insertItemWithObjectValue:wxNSStringWithWxString(szItem) atIndex:nIndex]; | |
307 | return nIndex; | |
308 | } | |
309 | ||
310 | void wxComboBox::DoSetItemClientData(int nIndex, void* pData) | |
311 | { | |
312 | m_Datas[nIndex] = pData; | |
313 | } | |
314 | ||
315 | void* wxComboBox::DoGetItemClientData(int nIndex) const | |
316 | { | |
317 | return m_Datas[nIndex]; | |
318 | } | |
319 | ||
320 | void wxComboBox::DoSetItemClientObject(int nIndex, wxClientData* pClientData) | |
321 | { | |
322 | m_Datas[nIndex] = (void*) pClientData; | |
323 | } | |
324 | ||
325 | wxClientData* wxComboBox::DoGetItemClientObject(int nIndex) const | |
326 | { | |
327 | return (wxClientData*) m_Datas[nIndex]; | |
328 | } | |
329 | ||
330 | #endif //wxUSE_COMBOBOX |