]> git.saurik.com Git - wxWidgets.git/blob - src/cocoa/combobox.mm
missing dllexport declaration added
[wxWidgets.git] / src / cocoa / combobox.mm
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: 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 // #include "wx/wxprec.h"
13
14 //
15 // Impl notes:
16 // There is no custom data source because doing so unnecessarily sacrifices
17 // some native autocompletion behavior (we would have to make our own -
18 // the SimpleComboBox sample does so in the developer folder that
19 // comes with OSX). One reason you might want this would be to have
20 // only one array or be able to display numbers returned by an NSNumber
21 // from the methods.
22 //
23 // One problem though is that wxCB_SORT isn't implemented...
24 //
25 // Also, not sure if it is correctly getting text field events since
26 // I'm using SetNSComboBox instead of SetNSTextField
27 //
28 // doWxEvent is really hackish... but since there's only one event...
29 //
30 // Ideas for future improvement - other notes:
31 // Combox w/o wxCB_DROPDOWN doesn't seem to be implementable
32 //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.
33 //wxCB_SORT is possible with data source
34 //
35 // setIntercellSpacing:/setItemHeight: to autoadjust to number of inserted items?
36 //
37 /*
38 //example of autocompletion from SimpleComboBox Example
39 // ==========================================================
40 // Combo box data source methods
41 // ==========================================================
42
43 - (int)numberOfItemsInComboBox:(NSComboBox *)aComboBox {
44 return [genres count];
45 }
46 - (id)comboBox:(NSComboBox *)aComboBox objectValueForItemAtIndex:(int)index {
47 return [genres objectAtIndex:index];
48 }
49 - (unsigned int)comboBox:(NSComboBox *)aComboBox indexOfItemWithStringValue:(NSString *)string {
50 return [genres indexOfObject: string];
51 }
52
53 - (NSString *) firstGenreMatchingPrefix:(NSString *)prefix {
54 NSString *string = nil;
55 NSString *lowercasePrefix = [prefix lowercaseString];
56 NSEnumerator *stringEnum = [genres objectEnumerator];
57 while ((string = [stringEnum nextObject])) {
58 if ([[string lowercaseString] hasPrefix: lowercasePrefix]) return string;
59 }
60 return nil;
61 }
62
63 - (NSString *)comboBox:(NSComboBox *)aComboBox completedString:(NSString *)inputString {
64 // This method is received after each character typed by the user, because we have checked the "completes" flag for genreComboBox in IB.
65 // 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.
66 NSString *candidate = [self firstGenreMatchingPrefix: inputString];
67 return (candidate ? candidate : inputString);
68 }
69 */
70
71 /////////////////////////////////////////////////////////////////////////////
72 // Name: cocoa/NSComboBox.mm
73 // Purpose: wxCocoaNSComboBox
74 // Author: Ryan Norton
75 // Modified by:
76 // Created: 2005/02/16
77 // RCS-ID: $Id:
78 // Copyright: (c) 2003 David Elliott
79 // Licence: wxWidgets licence
80 /////////////////////////////////////////////////////////////////////////////
81
82 // ============================================================================
83 // declarations
84 // ============================================================================
85
86 // ----------------------------------------------------------------------------
87 // headers
88 // ----------------------------------------------------------------------------
89
90 #include "wx/wxprec.h"
91 #if wxUSE_COMBOBOX
92
93 #ifndef WX_PRECOMP
94 #include "wx/window.h"
95 #endif // WX_PRECOMP
96
97 #include "wx/cocoa/ObjcPose.h"
98 #include "wx/combobox.h"
99
100 #import <AppKit/NSComboBox.h>
101 #import <Foundation/NSNotification.h>
102 #import <Foundation/NSString.h>
103
104 // ----------------------------------------------------------------------------
105 // globals
106 // ----------------------------------------------------------------------------
107 WX_IMPLEMENT_OBJC_INTERFACE_HASHMAP(NSComboBox)
108
109 void wxCocoaNSComboBox::AssociateNSComboBox(WX_NSComboBox cocoaNSComboBox)
110 {
111 if(cocoaNSComboBox)
112 {
113 sm_cocoaHash.insert(wxCocoaNSComboBoxHash::value_type(cocoaNSComboBox,this));
114
115 [[NSNotificationCenter defaultCenter] addObserver:(id)cocoaNSComboBox selector:@selector(comboBoxSelectionDidChange:) name:@"NSComboBoxSelectionDidChangeNotification" object:cocoaNSComboBox];
116 [[NSNotificationCenter defaultCenter] addObserver:(id)cocoaNSComboBox selector:@selector(comboBoxSelectionDidChange:) name:@"NSComboBoxSelectionIsChangingNotification" object:cocoaNSComboBox];
117 [[NSNotificationCenter defaultCenter] addObserver:(id)cocoaNSComboBox selector:@selector(comboBoxSelectionDidChange:) name:@"NSComboBoxWillDismissNotification" object:cocoaNSComboBox];
118 [[NSNotificationCenter defaultCenter] addObserver:(id)cocoaNSComboBox selector:@selector(comboBoxSelectionDidChange:) name:@"NSComboBoxWillPopUpNotification" object:cocoaNSComboBox];
119 }
120 }
121
122 void wxCocoaNSComboBox::DisassociateNSComboBox(WX_NSComboBox cocoaNSComboBox)
123 {
124 if(cocoaNSComboBox)
125 {
126 sm_cocoaHash.erase(cocoaNSComboBox);
127 [[NSNotificationCenter defaultCenter] removeObserver:(id)cocoaNSComboBox name:@"NSComboBoxSelectionDidChangeNotification" object:cocoaNSComboBox];
128 [[NSNotificationCenter defaultCenter] removeObserver:(id)cocoaNSComboBox name:@"NSComboBoxSelectionIsChangingNotification" object:cocoaNSComboBox];
129 [[NSNotificationCenter defaultCenter] removeObserver:(id)cocoaNSComboBox name:@"NSComboBoxWillDismissNotification" object:cocoaNSComboBox];
130 [[NSNotificationCenter defaultCenter] removeObserver:(id)cocoaNSComboBox name:@"NSComboBoxWillPopUpNotification" object:cocoaNSComboBox];
131 }
132 }
133
134 // ============================================================================
135 // @class wxPoserNSComboBox
136 // ============================================================================
137 @interface wxPoserNSComboBox : NSComboBox
138 {
139 }
140
141 - (void)comboBoxSelectionDidChange:(NSNotification *)notification;
142 - (void)comboBoxSelectionIsChanging:(NSNotification *)notification;
143 - (void)comboBoxWillDismiss:(NSNotification *)notification;
144 - (void)comboBoxWillPopUp:(NSNotification *)notification;
145 @end // wxPoserNSComboBox
146
147 //WX_IMPLEMENT_POSER(wxPoserNSComboBox);
148 @implementation wxPoserNSComboBox : NSComboBox
149
150 - (void)comboBoxSelectionDidChange:(NSNotification *)notification
151 {
152 wxCocoaNSComboBox *win = wxCocoaNSComboBox::GetFromCocoa(self);
153 win->doWxEvent(wxEVT_COMMAND_COMBOBOX_SELECTED);
154 }
155
156 - (void)comboBoxSelectionIsChanging:(NSNotification *)notification
157 {
158 //...
159 }
160
161 - (void)comboBoxWillDismiss:(NSNotification *)notification
162 {
163 //...
164 }
165
166 - (void)comboBoxWillPopUp:(NSNotification *)notification
167 {
168 //...
169 }
170
171 @end // implementation wxPoserNSComboBox
172
173 #include "wx/app.h"
174 #include "wx/combobox.h"
175 #include "wx/log.h"
176
177 #include "wx/cocoa/autorelease.h"
178 #include "wx/cocoa/string.h"
179
180 #import <AppKit/NSComboBox.h>
181
182 IMPLEMENT_DYNAMIC_CLASS(wxComboBox, wxTextCtrl)
183 BEGIN_EVENT_TABLE(wxComboBox, wxTextCtrl)
184 END_EVENT_TABLE()
185 WX_IMPLEMENT_COCOA_OWNER(wxComboBox,NSComboBox,NSTextField,NSView)
186
187 bool wxComboBox::Create(wxWindow *parent, wxWindowID winid,
188 const wxString& value,
189 const wxPoint& pos,
190 const wxSize& size,
191 const wxArrayString& choices,
192 long style,
193 const wxValidator& validator,
194 const wxString& name)
195 {
196 wxCArrayString chs(choices);
197
198 return Create(parent, winid, value, pos, size, chs.GetCount(),
199 chs.GetStrings(), style, validator, name);
200 }
201
202 bool wxComboBox::Create(wxWindow *parent, wxWindowID winid,
203 const wxString& value,
204 const wxPoint& pos,
205 const wxSize& size,
206 int n, const wxString choices[],
207 long style,
208 const wxValidator& validator,
209 const wxString& name)
210 {
211 wxAutoNSAutoreleasePool pool;
212 if(!CreateControl(parent,winid,pos,size,style,validator,name))
213 return false;
214
215 m_cocoaNSView = NULL;
216 SetNSComboBox([[wxPoserNSComboBox alloc] initWithFrame:MakeDefaultNSRect(size)]);
217 [m_cocoaNSView release];
218 [GetNSTextField() setStringValue:wxNSStringWithWxString(value.c_str())];
219 [GetNSControl() sizeToFit];
220 if(m_parent)
221 m_parent->CocoaAddChild(this);
222 SetInitialFrameRect(pos,size);
223
224 for(int i = 0; i < n; ++i)
225 wxComboBox::DoAppend(choices[i]);
226
227 [GetNSComboBox() setCompletes:true]; //autocomplete :)
228
229 return true;
230 }
231
232 wxComboBox::~wxComboBox()
233 {
234 DisassociateNSComboBox(GetNSComboBox());
235 }
236
237 void wxComboBox::doWxEvent(int nEvent)
238 {
239 wxCommandEvent event2(wxEVT_COMMAND_COMBOBOX_SELECTED, GetId() );
240 event2.SetInt(GetSelection());
241 event2.SetEventObject(this);
242 event2.SetString(GetStringSelection());
243 GetEventHandler()->ProcessEvent(event2);
244
245 // For consistency with MSW and GTK, also send a text updated event
246 // After all, the text is updated when a selection is made
247 wxCommandEvent TextEvent( wxEVT_COMMAND_TEXT_UPDATED, GetId() );
248 TextEvent.SetString( GetStringSelection() );
249 TextEvent.SetEventObject( this );
250 GetEventHandler()->ProcessEvent( TextEvent );
251 }
252
253
254 void wxComboBox::SetSelection(int nSelection)
255 {
256 [GetNSComboBox() selectItemAtIndex:nSelection];
257 }
258
259 wxString wxComboBox::GetStringSelection()
260 {
261 return wxStringWithNSString([GetNSComboBox() objectValueOfSelectedItem]);
262 }
263
264 void wxComboBox::Clear()
265 {
266 [GetNSComboBox() removeAllItems];
267 m_Datas.Clear();
268 }
269
270 void wxComboBox::Delete(int nIndex)
271 {
272 [GetNSComboBox() removeItemAtIndex:nIndex];
273 m_Datas.RemoveAt(nIndex);
274 }
275
276 int wxComboBox::GetCount() const
277 {
278 return [GetNSComboBox() numberOfItems];
279 }
280
281 wxString wxComboBox::GetString(int nIndex) const
282 { return wxStringWithNSString([GetNSComboBox() itemObjectValueAtIndex:nIndex]); }
283
284 void wxComboBox::SetString(int nIndex, const wxString& szString)
285 {
286 wxAutoNSAutoreleasePool pool;
287 //FIXME: There appears to be no "set item data" method - maybe
288 //an assignment would work?
289 [GetNSComboBox() removeItemAtIndex:nIndex];
290 [GetNSComboBox() insertItemWithObjectValue:wxNSStringWithWxString(szString) atIndex:nIndex];
291 }
292
293 int wxComboBox::FindString(const wxString& szItem) const
294 { return [GetNSComboBox() indexOfItemWithObjectValue:wxNSStringWithWxString(szItem)]; }
295
296 int wxComboBox::GetSelection() const
297 { return [GetNSComboBox() indexOfSelectedItem]; }
298
299 int wxComboBox::DoAppend(const wxString& szItem)
300 {
301 m_Datas.Add(NULL);
302 wxAutoNSAutoreleasePool pool;
303 [GetNSComboBox() addItemWithObjectValue:wxNSStringWithWxString(szItem)];
304 return [GetNSComboBox() numberOfItems];
305 }
306
307 int wxComboBox::DoInsert(const wxString& szItem, int nIndex)
308 {
309 m_Datas.Insert(NULL, nIndex);
310 wxAutoNSAutoreleasePool pool;
311 [GetNSComboBox() insertItemWithObjectValue:wxNSStringWithWxString(szItem) atIndex:nIndex];
312 return nIndex;
313 }
314
315 void wxComboBox::DoSetItemClientData(int nIndex, void* pData)
316 {
317 m_Datas[nIndex] = pData;
318 }
319
320 void* wxComboBox::DoGetItemClientData(int nIndex) const
321 {
322 return m_Datas[nIndex];
323 }
324
325 void wxComboBox::DoSetItemClientObject(int nIndex, wxClientData* pClientData)
326 {
327 m_Datas[nIndex] = (void*) pClientData;
328 }
329
330 wxClientData* wxComboBox::DoGetItemClientObject(int nIndex) const
331 {
332 return (wxClientData*) m_Datas[nIndex];
333 }
334
335 #endif //wxUSE_COMBOBOX