]> git.saurik.com Git - wxWidgets.git/blob - src/cocoa/combobox.mm
Initial work on virtual file system support for the WebKitGTK+ backend. It now suppor...
[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: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 //
13 // Impl notes:
14 // There is no custom data source because doing so unnecessarily sacrifices
15 // some native autocompletion behaviour (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 #include "wx/combobox.h"
82
83 #include "wx/cocoa/objc/objc_uniquifying.h"
84
85 #ifndef WX_PRECOMP
86 #include "wx/window.h"
87 #include "wx/log.h"
88 #include "wx/app.h"
89 #endif // WX_PRECOMP
90
91 #import <AppKit/NSComboBox.h>
92 #import <Foundation/NSNotification.h>
93 #import <Foundation/NSString.h>
94
95 // ----------------------------------------------------------------------------
96 // globals
97 // ----------------------------------------------------------------------------
98 WX_IMPLEMENT_OBJC_INTERFACE_HASHMAP(NSComboBox)
99
100 void wxCocoaNSComboBox::AssociateNSComboBox(WX_NSComboBox cocoaNSComboBox)
101 {
102 if(cocoaNSComboBox)
103 {
104 sm_cocoaHash.insert(wxCocoaNSComboBoxHash::value_type(cocoaNSComboBox,this));
105
106 [[NSNotificationCenter defaultCenter] addObserver:(id)cocoaNSComboBox selector:@selector(comboBoxSelectionDidChange:) name:@"NSComboBoxSelectionDidChangeNotification" object:cocoaNSComboBox];
107 [[NSNotificationCenter defaultCenter] addObserver:(id)cocoaNSComboBox selector:@selector(comboBoxSelectionDidChange:) name:@"NSComboBoxSelectionIsChangingNotification" object:cocoaNSComboBox];
108 [[NSNotificationCenter defaultCenter] addObserver:(id)cocoaNSComboBox selector:@selector(comboBoxSelectionDidChange:) name:@"NSComboBoxWillDismissNotification" object:cocoaNSComboBox];
109 [[NSNotificationCenter defaultCenter] addObserver:(id)cocoaNSComboBox selector:@selector(comboBoxSelectionDidChange:) name:@"NSComboBoxWillPopUpNotification" object:cocoaNSComboBox];
110 }
111 }
112
113 void wxCocoaNSComboBox::DisassociateNSComboBox(WX_NSComboBox cocoaNSComboBox)
114 {
115 if(cocoaNSComboBox)
116 {
117 sm_cocoaHash.erase(cocoaNSComboBox);
118 [[NSNotificationCenter defaultCenter] removeObserver:(id)cocoaNSComboBox name:@"NSComboBoxSelectionDidChangeNotification" object:cocoaNSComboBox];
119 [[NSNotificationCenter defaultCenter] removeObserver:(id)cocoaNSComboBox name:@"NSComboBoxSelectionIsChangingNotification" object:cocoaNSComboBox];
120 [[NSNotificationCenter defaultCenter] removeObserver:(id)cocoaNSComboBox name:@"NSComboBoxWillDismissNotification" object:cocoaNSComboBox];
121 [[NSNotificationCenter defaultCenter] removeObserver:(id)cocoaNSComboBox name:@"NSComboBoxWillPopUpNotification" object:cocoaNSComboBox];
122 }
123 }
124
125 // ============================================================================
126 // @class wxPoserNSComboBox
127 // ============================================================================
128 @interface wxPoserNSComboBox : NSComboBox
129 {
130 }
131
132 - (void)comboBoxSelectionDidChange:(NSNotification *)notification;
133 - (void)comboBoxSelectionIsChanging:(NSNotification *)notification;
134 - (void)comboBoxWillDismiss:(NSNotification *)notification;
135 - (void)comboBoxWillPopUp:(NSNotification *)notification;
136 @end // wxPoserNSComboBox
137 WX_DECLARE_GET_OBJC_CLASS(wxPoserNSComboBox,NSComboBox)
138
139 //WX_IMPLEMENT_POSER(wxPoserNSComboBox);
140 @implementation wxPoserNSComboBox : NSComboBox
141
142 - (void)comboBoxSelectionDidChange:(NSNotification *)notification
143 {
144 wxCocoaNSComboBox *win = wxCocoaNSComboBox::GetFromCocoa(self);
145 win->doWxEvent(wxEVT_COMMAND_COMBOBOX_SELECTED);
146 }
147
148 - (void)comboBoxSelectionIsChanging:(NSNotification *)notification
149 {
150 //...
151 }
152
153 - (void)comboBoxWillDismiss:(NSNotification *)notification
154 {
155 //...
156 }
157
158 - (void)comboBoxWillPopUp:(NSNotification *)notification
159 {
160 //...
161 }
162
163 @end // implementation wxPoserNSComboBox
164 WX_IMPLEMENT_GET_OBJC_CLASS(wxPoserNSComboBox,NSComboBox)
165
166 #include "wx/cocoa/autorelease.h"
167 #include "wx/cocoa/string.h"
168
169 #import <AppKit/NSComboBox.h>
170
171 BEGIN_EVENT_TABLE(wxComboBox, wxControl)
172 END_EVENT_TABLE()
173 WX_IMPLEMENT_COCOA_OWNER(wxComboBox,NSComboBox,NSTextField,NSView)
174 WX_IMPLEMENT_COCOA_OWNER(wxComboBox,NSTextField,NSControl,NSView)
175
176 bool wxComboBox::Create(wxWindow *parent, wxWindowID winid,
177 const wxString& value,
178 const wxPoint& pos,
179 const wxSize& size,
180 const wxArrayString& choices,
181 long style,
182 const wxValidator& validator,
183 const wxString& name)
184 {
185 wxCArrayString chs(choices);
186
187 return Create(parent, winid, value, pos, size, chs.GetCount(),
188 chs.GetStrings(), style, validator, name);
189 }
190
191 bool wxComboBox::Create(wxWindow *parent, wxWindowID winid,
192 const wxString& value,
193 const wxPoint& pos,
194 const wxSize& size,
195 int n, const wxString choices[],
196 long style,
197 const wxValidator& validator,
198 const wxString& name)
199 {
200 wxAutoNSAutoreleasePool pool;
201 if(!CreateControl(parent,winid,pos,size,style,validator,name))
202 return false;
203
204 m_cocoaNSView = NULL;
205 SetNSComboBox([[WX_GET_OBJC_CLASS(wxPoserNSComboBox) alloc] initWithFrame:MakeDefaultNSRect(size)]);
206 [m_cocoaNSView release];
207 [GetNSTextField() setStringValue:wxNSStringWithWxString(value.c_str())];
208 [GetNSControl() sizeToFit];
209 if(m_parent)
210 m_parent->CocoaAddChild(this);
211 SetInitialFrameRect(pos,size);
212
213 wxComboBox::Append(n, choices);
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 HandleWindowEvent(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 HandleWindowEvent( 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::DoClear()
253 {
254 [GetNSComboBox() removeAllItems];
255 m_Datas.Clear();
256 }
257
258 void wxComboBox::DoDeleteOneItem(unsigned int n)
259 {
260 [GetNSComboBox() removeItemAtIndex:n];
261 m_Datas.RemoveAt(n);
262 }
263
264 unsigned int wxComboBox::GetCount() const
265 {
266 return (unsigned int)[GetNSComboBox() numberOfItems];
267 }
268
269 wxString wxComboBox::GetString(unsigned int nIndex) const
270 {
271 return wxStringWithNSString([GetNSComboBox() itemObjectValueAtIndex:nIndex]);
272 }
273
274 void wxComboBox::SetString(unsigned 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::DoInsertItems(const wxArrayStringsAdapter& items,
295 unsigned int pos,
296 void **clientData,
297 wxClientDataType type)
298 {
299 wxAutoNSAutoreleasePool pool;
300 const unsigned int numITems = items.GetCount();
301 for ( unsigned int i = 0; i < numITems; ++i, ++pos )
302 {
303 [GetNSComboBox() insertItemWithObjectValue:wxNSStringWithWxString(items[i]) atIndex:(pos)];
304 m_Datas.Insert(NULL, pos);
305 AssignNewItemClientData(pos, clientData, i, type);
306 }
307 return pos - 1;
308 }
309
310 void wxComboBox::DoSetItemClientData(unsigned int nIndex, void* pData)
311 {
312 m_Datas[nIndex] = pData;
313 }
314
315 void* wxComboBox::DoGetItemClientData(unsigned int nIndex) const
316 {
317 return m_Datas[nIndex];
318 }
319
320 /////////////////////////////////////////////////////////////////////////////
321 // wxTextEntry virtual implementations:
322
323 void wxComboBox::WriteText(wxString const&)
324 {
325 }
326
327 wxString wxComboBox::GetValue() const
328 {
329 wxAutoNSAutoreleasePool pool;
330 return wxStringWithNSString([GetNSTextField() stringValue]);
331 }
332
333 void wxComboBox::Remove(long, long)
334 {
335 }
336
337 void wxComboBox::Cut()
338 {
339 }
340
341 void wxComboBox::Copy()
342 {
343 }
344
345 void wxComboBox::Paste()
346 {
347 }
348
349 void wxComboBox::Undo()
350 {
351 }
352
353 void wxComboBox::Redo()
354 {
355 }
356
357 bool wxComboBox::CanUndo() const
358 {
359 return false;
360 }
361
362 bool wxComboBox::CanRedo() const
363 {
364 return false;
365 }
366
367 void wxComboBox::SetInsertionPoint(long)
368 {
369 }
370
371 long wxComboBox::GetInsertionPoint() const
372 {
373 return 0;
374 }
375
376 wxTextPos wxComboBox::GetLastPosition() const
377 {
378 // working - returns the size of the wxString
379 return (long)(GetValue().Len());
380 }
381
382 void wxComboBox::SetSelection(long, long)
383 {
384 }
385
386 void wxComboBox::GetSelection(long*, long*) const
387 {
388 }
389
390 bool wxComboBox::IsEditable() const
391 {
392 return [GetNSTextField() isEditable];
393 }
394
395 void wxComboBox::SetEditable(bool editable)
396 {
397 // first ensure that the current value is stored (in case the user had not finished editing
398 // before SetEditable(FALSE) was called)
399 DoSetValue(GetValue(),1);
400
401 [GetNSTextField() setEditable: editable];
402
403 // forces the focus on the textctrl to be lost - while focus is still maintained
404 // after SetEditable(FALSE) the user may still edit the control
405 // (might not the best way to do this..)
406 [GetNSTextField() abortEditing];
407 }
408
409 #endif // wxUSE_COMBOBOX