]> git.saurik.com Git - wxWidgets.git/blob - src/cocoa/combobox.mm
prevent GTK+ from changing locale when wxUSE_INTL==0, this makes the behaviour consis...
[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
79 #if wxUSE_COMBOBOX
80
81 #include "wx/combobox.h"
82
83 #ifndef WX_PRECOMP
84 #include "wx/window.h"
85 #include "wx/log.h"
86 #include "wx/app.h"
87 #endif // WX_PRECOMP
88
89 #include "wx/cocoa/ObjcPose.h"
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
138 //WX_IMPLEMENT_POSER(wxPoserNSComboBox);
139 @implementation wxPoserNSComboBox : NSComboBox
140
141 - (void)comboBoxSelectionDidChange:(NSNotification *)notification
142 {
143 wxCocoaNSComboBox *win = wxCocoaNSComboBox::GetFromCocoa(self);
144 win->doWxEvent(wxEVT_COMMAND_COMBOBOX_SELECTED);
145 }
146
147 - (void)comboBoxSelectionIsChanging:(NSNotification *)notification
148 {
149 //...
150 }
151
152 - (void)comboBoxWillDismiss:(NSNotification *)notification
153 {
154 //...
155 }
156
157 - (void)comboBoxWillPopUp:(NSNotification *)notification
158 {
159 //...
160 }
161
162 @end // implementation wxPoserNSComboBox
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(unsigned int n)
258 {
259 [GetNSComboBox() removeItemAtIndex:n];
260 m_Datas.RemoveAt(n);
261 }
262
263 unsigned int wxComboBox::GetCount() const
264 {
265 return (unsigned int)[GetNSComboBox() numberOfItems];
266 }
267
268 wxString wxComboBox::GetString(unsigned int nIndex) const
269 {
270 return wxStringWithNSString([GetNSComboBox() itemObjectValueAtIndex:nIndex]);
271 }
272
273 void wxComboBox::SetString(unsigned 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, unsigned int nIndex)
302 {
303 m_Datas.Insert(NULL, nIndex);
304 wxAutoNSAutoreleasePool pool;
305 [GetNSComboBox() insertItemWithObjectValue:wxNSStringWithWxString(szItem) atIndex:nIndex];
306 return (int)nIndex;
307 }
308
309 void wxComboBox::DoSetItemClientData(unsigned int nIndex, void* pData)
310 {
311 m_Datas[nIndex] = pData;
312 }
313
314 void* wxComboBox::DoGetItemClientData(unsigned int nIndex) const
315 {
316 return m_Datas[nIndex];
317 }
318
319 void wxComboBox::DoSetItemClientObject(unsigned int nIndex, wxClientData* pClientData)
320 {
321 m_Datas[nIndex] = (void*) pClientData;
322 }
323
324 wxClientData* wxComboBox::DoGetItemClientObject(unsigned int nIndex) const
325 {
326 return (wxClientData*) m_Datas[nIndex];
327 }
328
329 #endif //wxUSE_COMBOBOX