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