]> git.saurik.com Git - wxWidgets.git/blame - src/cocoa/combobox.mm
correcting DoGetPosition for windows with borders
[wxWidgets.git] / src / cocoa / combobox.mm
CommitLineData
421a8431
DE
1/////////////////////////////////////////////////////////////////////////////
2// Name: cocoa/combobox.mm
3// Purpose: wxComboBox
8f248607 4// Author: Ryan Norton
421a8431 5// Modified by:
8f248607 6// Created: 2005/02/16
a6660cbb 7// RCS-ID: $Id$
421a8431 8// Copyright: (c) 2003 David Elliott
065e208e 9// Licence: wxWidgets licence
421a8431
DE
10/////////////////////////////////////////////////////////////////////////////
11
acd3dca8 12// #include "wx/wxprec.h"
2ee09b55 13
8f248607
RN
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*/
2ee09b55
DE
70#if wxUSE_COMBOBOX
71
8f248607
RN
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// ----------------------------------------------------------------------------
106WX_IMPLEMENT_OBJC_INTERFACE_HASHMAP(NSComboBox)
107
108void 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
121void 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
421a8431
DE
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
181IMPLEMENT_DYNAMIC_CLASS(wxComboBox, wxTextCtrl)
182BEGIN_EVENT_TABLE(wxComboBox, wxTextCtrl)
183END_EVENT_TABLE()
8f248607 184WX_IMPLEMENT_COCOA_OWNER(wxComboBox,NSComboBox,NSTextField,NSView)
421a8431 185
584ad2a3
MB
186bool 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
421a8431
DE
201bool 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;
8f248607 213
421a8431 214 m_cocoaNSView = NULL;
8f248607 215 SetNSComboBox([[wxPoserNSComboBox alloc] initWithFrame:MakeDefaultNSRect(size)]);
421a8431
DE
216 [m_cocoaNSView release];
217 [GetNSTextField() setStringValue:wxNSStringWithWxString(value.c_str())];
218 [GetNSControl() sizeToFit];
219 if(m_parent)
220 m_parent->CocoaAddChild(this);
8d656ea9
DE
221 SetInitialFrameRect(pos,size);
222
8f248607
RN
223 for(int i = 0; i < n; ++i)
224 wxComboBox::DoAppend(choices[i]);
225
226 [GetNSComboBox() setCompletes:true]; //autocomplete :)
227
421a8431
DE
228 return true;
229}
230
231wxComboBox::~wxComboBox()
232{
8f248607 233 DisassociateNSComboBox(GetNSComboBox());
421a8431
DE
234}
235
8f248607 236void wxComboBox::doWxEvent(int nEvent)
421a8431 237{
8f248607
RN
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
253void wxComboBox::SetSelection(int nSelection)
254{
255 [GetNSComboBox() selectItemAtIndex:nSelection];
421a8431
DE
256}
257
258wxString wxComboBox::GetStringSelection()
259{
8f248607 260 return wxStringWithNSString([GetNSComboBox() objectValueOfSelectedItem]);
421a8431
DE
261}
262
421a8431
DE
263void wxComboBox::Clear()
264{
8f248607
RN
265 [GetNSComboBox() removeAllItems];
266 m_Datas.Clear();
421a8431
DE
267}
268
8f248607 269void wxComboBox::Delete(int nIndex)
421a8431 270{
8f248607
RN
271 [GetNSComboBox() removeItemAtIndex:nIndex];
272 m_Datas.RemoveAt(nIndex);
421a8431
DE
273}
274
275int wxComboBox::GetCount() const
276{
8f248607 277 return [GetNSComboBox() numberOfItems];
421a8431
DE
278}
279
8f248607
RN
280wxString wxComboBox::GetString(int nIndex) const
281{ return wxStringWithNSString([GetNSComboBox() itemObjectValueAtIndex:nIndex]); }
421a8431 282
8f248607
RN
283void 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];
421a8431
DE
290}
291
8f248607
RN
292int wxComboBox::FindString(const wxString& szItem) const
293{ return [GetNSComboBox() indexOfItemWithObjectValue:wxNSStringWithWxString(szItem)]; }
421a8431
DE
294
295int wxComboBox::GetSelection() const
8f248607 296{ return [GetNSComboBox() indexOfSelectedItem]; }
421a8431 297
8f248607 298int wxComboBox::DoAppend(const wxString& szItem)
421a8431 299{
8f248607
RN
300 m_Datas.Add(NULL);
301 wxAutoNSAutoreleasePool pool;
302 [GetNSComboBox() addItemWithObjectValue:wxNSStringWithWxString(szItem)];
303 return [GetNSComboBox() numberOfItems];
421a8431
DE
304}
305
8f248607 306int wxComboBox::DoInsert(const wxString& szItem, int nIndex)
421a8431 307{
8f248607
RN
308 m_Datas.Insert(NULL, nIndex);
309 wxAutoNSAutoreleasePool pool;
310 [GetNSComboBox() insertItemWithObjectValue:wxNSStringWithWxString(szItem) atIndex:nIndex];
311 return nIndex;
421a8431
DE
312}
313
8f248607 314void wxComboBox::DoSetItemClientData(int nIndex, void* pData)
421a8431 315{
8f248607 316 m_Datas[nIndex] = pData;
421a8431
DE
317}
318
8f248607 319void* wxComboBox::DoGetItemClientData(int nIndex) const
421a8431 320{
8f248607 321 return m_Datas[nIndex];
421a8431
DE
322}
323
8f248607 324void wxComboBox::DoSetItemClientObject(int nIndex, wxClientData* pClientData)
421a8431 325{
8f248607 326 m_Datas[nIndex] = (void*) pClientData;
421a8431
DE
327}
328
8f248607 329wxClientData* wxComboBox::DoGetItemClientObject(int nIndex) const
421a8431 330{
8f248607 331 return (wxClientData*) m_Datas[nIndex];
421a8431
DE
332}
333
2ee09b55 334#endif //wxUSE_COMBOBOX