]> git.saurik.com Git - wxWidgets.git/blob - src/cocoa/combobox.mm
Update copyright year in ctrl+alt+mclick dialog
[wxWidgets.git] / src / cocoa / combobox.mm
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 #if wxUSE_COMBOBOX
79
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));
101
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
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()
172 WX_IMPLEMENT_COCOA_OWNER(wxComboBox,NSComboBox,NSTextField,NSView)
173
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
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;
201
202 m_cocoaNSView = NULL;
203 SetNSComboBox([[wxPoserNSComboBox alloc] initWithFrame:MakeDefaultNSRect(size)]);
204 [m_cocoaNSView release];
205 [GetNSTextField() setStringValue:wxNSStringWithWxString(value.c_str())];
206 [GetNSControl() sizeToFit];
207 if(m_parent)
208 m_parent->CocoaAddChild(this);
209 SetInitialFrameRect(pos,size);
210
211 for(int i = 0; i < n; ++i)
212 wxComboBox::DoAppend(choices[i]);
213
214 [GetNSComboBox() setCompletes:true]; //autocomplete :)
215
216 return true;
217 }
218
219 wxComboBox::~wxComboBox()
220 {
221 DisassociateNSComboBox(GetNSComboBox());
222 }
223
224 void wxComboBox::doWxEvent(int nEvent)
225 {
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];
244 }
245
246 wxString wxComboBox::GetStringSelection()
247 {
248 return wxStringWithNSString([GetNSComboBox() objectValueOfSelectedItem]);
249 }
250
251 void wxComboBox::Clear()
252 {
253 [GetNSComboBox() removeAllItems];
254 m_Datas.Clear();
255 }
256
257 void wxComboBox::Delete(int nIndex)
258 {
259 [GetNSComboBox() removeItemAtIndex:nIndex];
260 m_Datas.RemoveAt(nIndex);
261 }
262
263 int wxComboBox::GetCount() const
264 {
265 return [GetNSComboBox() numberOfItems];
266 }
267
268 wxString wxComboBox::GetString(int nIndex) const
269 {
270 return wxStringWithNSString([GetNSComboBox() itemObjectValueAtIndex:nIndex]);
271 }
272
273 void wxComboBox::SetString(int nIndex, const wxString& szString)
274 {
275 wxAutoNSAutoreleasePool pool;
276 //FIXME: There appears to be no "set item data" method - maybe
277 //an assignment would work?
278 [GetNSComboBox() removeItemAtIndex:nIndex];
279 [GetNSComboBox() insertItemWithObjectValue:wxNSStringWithWxString(szString) atIndex:nIndex];
280 }
281
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 }
287
288 int wxComboBox::GetSelection() const
289 {
290 return [GetNSComboBox() indexOfSelectedItem];
291 }
292
293 int wxComboBox::DoAppend(const wxString& szItem)
294 {
295 m_Datas.Add(NULL);
296 wxAutoNSAutoreleasePool pool;
297 [GetNSComboBox() addItemWithObjectValue:wxNSStringWithWxString(szItem)];
298 return [GetNSComboBox() numberOfItems];
299 }
300
301 int wxComboBox::DoInsert(const wxString& szItem, int nIndex)
302 {
303 m_Datas.Insert(NULL, nIndex);
304 wxAutoNSAutoreleasePool pool;
305 [GetNSComboBox() insertItemWithObjectValue:wxNSStringWithWxString(szItem) atIndex:nIndex];
306 return nIndex;
307 }
308
309 void wxComboBox::DoSetItemClientData(int nIndex, void* pData)
310 {
311 m_Datas[nIndex] = pData;
312 }
313
314 void* wxComboBox::DoGetItemClientData(int nIndex) const
315 {
316 return m_Datas[nIndex];
317 }
318
319 void wxComboBox::DoSetItemClientObject(int nIndex, wxClientData* pClientData)
320 {
321 m_Datas[nIndex] = (void*) pClientData;
322 }
323
324 wxClientData* wxComboBox::DoGetItemClientObject(int nIndex) const
325 {
326 return (wxClientData*) m_Datas[nIndex];
327 }
328
329 #endif //wxUSE_COMBOBOX