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