]>
Commit | Line | Data |
---|---|---|
421a8431 | 1 | ///////////////////////////////////////////////////////////////////////////// |
11e62fe6 | 2 | // Name: src/cocoa/combobox.mm |
421a8431 | 3 | // Purpose: wxComboBox |
11e62fe6 | 4 | // Author: Ryan Norton |
421a8431 | 5 | // Modified by: |
8f248607 | 6 | // Created: 2005/02/16 |
a6660cbb | 7 | // RCS-ID: $Id$ |
421a8431 | 8 | // Copyright: (c) 2003 David Elliott |
11e62fe6 | 9 | // Licence: wxWidgets licence |
421a8431 DE |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
8f248607 RN |
12 | // |
13 | // Impl notes: | |
3103e8a9 | 14 | // There is no custom data source because doing so unnecessarily sacrifices |
11e62fe6 | 15 | // some native autocompletion behavior (we would have to make our own - |
8f248607 RN |
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 | |
3103e8a9 | 18 | // only one array or be able to display numbers returned by an NSNumber |
8f248607 RN |
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: | |
11e62fe6 WS |
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 | |
8f248607 RN |
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])) { | |
11e62fe6 | 56 | if ([[string lowercaseString] hasPrefix: lowercasePrefix]) return string; |
8f248607 RN |
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 | */ | |
2ee09b55 | 68 | |
8f248607 RN |
69 | // ============================================================================ |
70 | // declarations | |
71 | // ============================================================================ | |
72 | ||
73 | // ---------------------------------------------------------------------------- | |
74 | // headers | |
75 | // ---------------------------------------------------------------------------- | |
76 | ||
77 | #include "wx/wxprec.h" | |
061896d1 DE |
78 | #if wxUSE_COMBOBOX |
79 | ||
8f248607 RN |
80 | #ifndef WX_PRECOMP |
81 | #include "wx/window.h" | |
82 | #endif // WX_PRECOMP | |
83 | ||
84 | #include "wx/cocoa/ObjcPose.h" | |
85 | #include "wx/combobox.h" | |
86 | ||
87 | #import <AppKit/NSComboBox.h> | |
88 | #import <Foundation/NSNotification.h> | |
89 | #import <Foundation/NSString.h> | |
90 | ||
91 | // ---------------------------------------------------------------------------- | |
92 | // globals | |
93 | // ---------------------------------------------------------------------------- | |
94 | WX_IMPLEMENT_OBJC_INTERFACE_HASHMAP(NSComboBox) | |
95 | ||
96 | void wxCocoaNSComboBox::AssociateNSComboBox(WX_NSComboBox cocoaNSComboBox) | |
97 | { | |
98 | if(cocoaNSComboBox) | |
99 | { | |
100 | sm_cocoaHash.insert(wxCocoaNSComboBoxHash::value_type(cocoaNSComboBox,this)); | |
11e62fe6 | 101 | |
8f248607 RN |
102 | [[NSNotificationCenter defaultCenter] addObserver:(id)cocoaNSComboBox selector:@selector(comboBoxSelectionDidChange:) name:@"NSComboBoxSelectionDidChangeNotification" object:cocoaNSComboBox]; |
103 | [[NSNotificationCenter defaultCenter] addObserver:(id)cocoaNSComboBox selector:@selector(comboBoxSelectionDidChange:) name:@"NSComboBoxSelectionIsChangingNotification" object:cocoaNSComboBox]; | |
104 | [[NSNotificationCenter defaultCenter] addObserver:(id)cocoaNSComboBox selector:@selector(comboBoxSelectionDidChange:) name:@"NSComboBoxWillDismissNotification" object:cocoaNSComboBox]; | |
105 | [[NSNotificationCenter defaultCenter] addObserver:(id)cocoaNSComboBox selector:@selector(comboBoxSelectionDidChange:) name:@"NSComboBoxWillPopUpNotification" object:cocoaNSComboBox]; | |
106 | } | |
107 | } | |
108 | ||
109 | void wxCocoaNSComboBox::DisassociateNSComboBox(WX_NSComboBox cocoaNSComboBox) | |
110 | { | |
111 | if(cocoaNSComboBox) | |
112 | { | |
113 | sm_cocoaHash.erase(cocoaNSComboBox); | |
114 | [[NSNotificationCenter defaultCenter] removeObserver:(id)cocoaNSComboBox name:@"NSComboBoxSelectionDidChangeNotification" object:cocoaNSComboBox]; | |
115 | [[NSNotificationCenter defaultCenter] removeObserver:(id)cocoaNSComboBox name:@"NSComboBoxSelectionIsChangingNotification" object:cocoaNSComboBox]; | |
116 | [[NSNotificationCenter defaultCenter] removeObserver:(id)cocoaNSComboBox name:@"NSComboBoxWillDismissNotification" object:cocoaNSComboBox]; | |
117 | [[NSNotificationCenter defaultCenter] removeObserver:(id)cocoaNSComboBox name:@"NSComboBoxWillPopUpNotification" object:cocoaNSComboBox]; | |
118 | } | |
119 | } | |
120 | ||
121 | // ============================================================================ | |
122 | // @class wxPoserNSComboBox | |
123 | // ============================================================================ | |
124 | @interface wxPoserNSComboBox : NSComboBox | |
125 | { | |
126 | } | |
127 | ||
128 | - (void)comboBoxSelectionDidChange:(NSNotification *)notification; | |
129 | - (void)comboBoxSelectionIsChanging:(NSNotification *)notification; | |
130 | - (void)comboBoxWillDismiss:(NSNotification *)notification; | |
131 | - (void)comboBoxWillPopUp:(NSNotification *)notification; | |
132 | @end // wxPoserNSComboBox | |
133 | ||
134 | //WX_IMPLEMENT_POSER(wxPoserNSComboBox); | |
135 | @implementation wxPoserNSComboBox : NSComboBox | |
136 | ||
137 | - (void)comboBoxSelectionDidChange:(NSNotification *)notification | |
138 | { | |
139 | wxCocoaNSComboBox *win = wxCocoaNSComboBox::GetFromCocoa(self); | |
140 | win->doWxEvent(wxEVT_COMMAND_COMBOBOX_SELECTED); | |
141 | } | |
142 | ||
143 | - (void)comboBoxSelectionIsChanging:(NSNotification *)notification | |
144 | { | |
145 | //... | |
146 | } | |
147 | ||
148 | - (void)comboBoxWillDismiss:(NSNotification *)notification | |
149 | { | |
150 | //... | |
151 | } | |
152 | ||
153 | - (void)comboBoxWillPopUp:(NSNotification *)notification | |
154 | { | |
155 | //... | |
156 | } | |
157 | ||
158 | @end // implementation wxPoserNSComboBox | |
159 | ||
421a8431 DE |
160 | #include "wx/app.h" |
161 | #include "wx/combobox.h" | |
162 | #include "wx/log.h" | |
163 | ||
164 | #include "wx/cocoa/autorelease.h" | |
165 | #include "wx/cocoa/string.h" | |
166 | ||
167 | #import <AppKit/NSComboBox.h> | |
168 | ||
169 | IMPLEMENT_DYNAMIC_CLASS(wxComboBox, wxTextCtrl) | |
170 | BEGIN_EVENT_TABLE(wxComboBox, wxTextCtrl) | |
171 | END_EVENT_TABLE() | |
8f248607 | 172 | WX_IMPLEMENT_COCOA_OWNER(wxComboBox,NSComboBox,NSTextField,NSView) |
421a8431 | 173 | |
584ad2a3 MB |
174 | bool wxComboBox::Create(wxWindow *parent, wxWindowID winid, |
175 | const wxString& value, | |
176 | const wxPoint& pos, | |
177 | const wxSize& size, | |
178 | const wxArrayString& choices, | |
179 | long style, | |
180 | const wxValidator& validator, | |
181 | const wxString& name) | |
182 | { | |
183 | wxCArrayString chs(choices); | |
184 | ||
185 | return Create(parent, winid, value, pos, size, chs.GetCount(), | |
186 | chs.GetStrings(), style, validator, name); | |
187 | } | |
188 | ||
421a8431 DE |
189 | bool wxComboBox::Create(wxWindow *parent, wxWindowID winid, |
190 | const wxString& value, | |
191 | const wxPoint& pos, | |
192 | const wxSize& size, | |
193 | int n, const wxString choices[], | |
194 | long style, | |
195 | const wxValidator& validator, | |
196 | const wxString& name) | |
197 | { | |
198 | wxAutoNSAutoreleasePool pool; | |
199 | if(!CreateControl(parent,winid,pos,size,style,validator,name)) | |
200 | return false; | |
11e62fe6 | 201 | |
421a8431 | 202 | m_cocoaNSView = NULL; |
8f248607 | 203 | SetNSComboBox([[wxPoserNSComboBox alloc] initWithFrame:MakeDefaultNSRect(size)]); |
421a8431 DE |
204 | [m_cocoaNSView release]; |
205 | [GetNSTextField() setStringValue:wxNSStringWithWxString(value.c_str())]; | |
206 | [GetNSControl() sizeToFit]; | |
207 | if(m_parent) | |
208 | m_parent->CocoaAddChild(this); | |
8d656ea9 DE |
209 | SetInitialFrameRect(pos,size); |
210 | ||
8f248607 RN |
211 | for(int i = 0; i < n; ++i) |
212 | wxComboBox::DoAppend(choices[i]); | |
11e62fe6 | 213 | |
8f248607 | 214 | [GetNSComboBox() setCompletes:true]; //autocomplete :) |
11e62fe6 | 215 | |
421a8431 DE |
216 | return true; |
217 | } | |
218 | ||
219 | wxComboBox::~wxComboBox() | |
220 | { | |
8f248607 | 221 | DisassociateNSComboBox(GetNSComboBox()); |
421a8431 DE |
222 | } |
223 | ||
8f248607 | 224 | void wxComboBox::doWxEvent(int nEvent) |
421a8431 | 225 | { |
8f248607 RN |
226 | wxCommandEvent event2(wxEVT_COMMAND_COMBOBOX_SELECTED, GetId() ); |
227 | event2.SetInt(GetSelection()); | |
228 | event2.SetEventObject(this); | |
229 | event2.SetString(GetStringSelection()); | |
230 | GetEventHandler()->ProcessEvent(event2); | |
231 | ||
232 | // For consistency with MSW and GTK, also send a text updated event | |
233 | // After all, the text is updated when a selection is made | |
234 | wxCommandEvent TextEvent( wxEVT_COMMAND_TEXT_UPDATED, GetId() ); | |
235 | TextEvent.SetString( GetStringSelection() ); | |
236 | TextEvent.SetEventObject( this ); | |
237 | GetEventHandler()->ProcessEvent( TextEvent ); | |
238 | } | |
239 | ||
240 | ||
241 | void wxComboBox::SetSelection(int nSelection) | |
242 | { | |
243 | [GetNSComboBox() selectItemAtIndex:nSelection]; | |
421a8431 DE |
244 | } |
245 | ||
246 | wxString wxComboBox::GetStringSelection() | |
247 | { | |
8f248607 | 248 | return wxStringWithNSString([GetNSComboBox() objectValueOfSelectedItem]); |
421a8431 DE |
249 | } |
250 | ||
421a8431 DE |
251 | void wxComboBox::Clear() |
252 | { | |
8f248607 RN |
253 | [GetNSComboBox() removeAllItems]; |
254 | m_Datas.Clear(); | |
421a8431 DE |
255 | } |
256 | ||
8f248607 | 257 | void wxComboBox::Delete(int nIndex) |
421a8431 | 258 | { |
8f248607 RN |
259 | [GetNSComboBox() removeItemAtIndex:nIndex]; |
260 | m_Datas.RemoveAt(nIndex); | |
421a8431 DE |
261 | } |
262 | ||
263 | int wxComboBox::GetCount() const | |
264 | { | |
8f248607 | 265 | return [GetNSComboBox() numberOfItems]; |
421a8431 DE |
266 | } |
267 | ||
8f248607 | 268 | wxString wxComboBox::GetString(int nIndex) const |
11e62fe6 WS |
269 | { |
270 | return wxStringWithNSString([GetNSComboBox() itemObjectValueAtIndex:nIndex]); | |
271 | } | |
421a8431 | 272 | |
8f248607 | 273 | void wxComboBox::SetString(int nIndex, const wxString& szString) |
11e62fe6 | 274 | { |
8f248607 RN |
275 | wxAutoNSAutoreleasePool pool; |
276 | //FIXME: There appears to be no "set item data" method - maybe | |
277 | //an assignment would work? | |
278 | [GetNSComboBox() removeItemAtIndex:nIndex]; | |
11e62fe6 | 279 | [GetNSComboBox() insertItemWithObjectValue:wxNSStringWithWxString(szString) atIndex:nIndex]; |
421a8431 DE |
280 | } |
281 | ||
11e62fe6 WS |
282 | int wxComboBox::FindString(const wxString& szItem, bool bCase) const |
283 | { | |
284 | // FIXME: use wxItemContainerImmutable::FindString for bCase parameter | |
285 | return [GetNSComboBox() indexOfItemWithObjectValue:wxNSStringWithWxString(szItem)]; | |
286 | } | |
421a8431 DE |
287 | |
288 | int wxComboBox::GetSelection() const | |
11e62fe6 WS |
289 | { |
290 | return [GetNSComboBox() indexOfSelectedItem]; | |
291 | } | |
421a8431 | 292 | |
8f248607 | 293 | int wxComboBox::DoAppend(const wxString& szItem) |
421a8431 | 294 | { |
8f248607 RN |
295 | m_Datas.Add(NULL); |
296 | wxAutoNSAutoreleasePool pool; | |
297 | [GetNSComboBox() addItemWithObjectValue:wxNSStringWithWxString(szItem)]; | |
298 | return [GetNSComboBox() numberOfItems]; | |
421a8431 DE |
299 | } |
300 | ||
8f248607 | 301 | int wxComboBox::DoInsert(const wxString& szItem, int nIndex) |
421a8431 | 302 | { |
8f248607 RN |
303 | m_Datas.Insert(NULL, nIndex); |
304 | wxAutoNSAutoreleasePool pool; | |
305 | [GetNSComboBox() insertItemWithObjectValue:wxNSStringWithWxString(szItem) atIndex:nIndex]; | |
306 | return nIndex; | |
421a8431 DE |
307 | } |
308 | ||
8f248607 | 309 | void wxComboBox::DoSetItemClientData(int nIndex, void* pData) |
421a8431 | 310 | { |
8f248607 | 311 | m_Datas[nIndex] = pData; |
421a8431 DE |
312 | } |
313 | ||
8f248607 | 314 | void* wxComboBox::DoGetItemClientData(int nIndex) const |
421a8431 | 315 | { |
8f248607 | 316 | return m_Datas[nIndex]; |
421a8431 DE |
317 | } |
318 | ||
8f248607 | 319 | void wxComboBox::DoSetItemClientObject(int nIndex, wxClientData* pClientData) |
421a8431 | 320 | { |
8f248607 | 321 | m_Datas[nIndex] = (void*) pClientData; |
421a8431 DE |
322 | } |
323 | ||
8f248607 | 324 | wxClientData* wxComboBox::DoGetItemClientObject(int nIndex) const |
421a8431 | 325 | { |
8f248607 | 326 | return (wxClientData*) m_Datas[nIndex]; |
421a8431 DE |
327 | } |
328 | ||
2ee09b55 | 329 | #endif //wxUSE_COMBOBOX |