Provide shorter synonyms for wxEVT_XXX constants.
[wxWidgets.git] / src / osx / cocoa / combobox.mm
1 /////////////////////////////////////////////////////////////////////////////
2 // Name:        src/osx/cocoa/combobox.mm
3 // Purpose:     wxChoice
4 // Author:      Stefan Csomor
5 // Modified by:
6 // Created:     1998-01-01
7 // RCS-ID:      $Id$
8 // Copyright:   (c) Stefan Csomor
9 // Licence:     wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #include "wx/wxprec.h"
13
14 #if wxUSE_COMBOBOX
15
16 #include "wx/combobox.h"
17 #include "wx/evtloop.h"
18
19 #ifndef WX_PRECOMP
20     #include "wx/menu.h"
21     #include "wx/dcclient.h"
22 #endif
23
24 #include "wx/osx/cocoa/private/textimpl.h"
25
26 // work in progress
27
28 @interface wxNSTableDataSource : NSObject wxOSX_10_6_AND_LATER(<NSComboBoxDataSource>)
29 {
30     wxNSComboBoxControl* impl;
31 }
32
33 - (NSInteger)numberOfItemsInComboBox:(NSComboBox *)aComboBox;
34 - (id)comboBox:(NSComboBox *)aComboBox objectValueForItemAtIndex:(NSInteger)index;
35
36 @end
37
38
39 @interface wxNSComboBox : NSComboBox
40 {
41 }
42
43 @end
44
45 @implementation wxNSComboBox
46
47 + (void)initialize
48 {
49     static BOOL initialized = NO;
50     if (!initialized) 
51     {
52         initialized = YES;
53         wxOSXCocoaClassAddWXMethods( self );
54     }
55 }
56
57 - (void)controlTextDidChange:(NSNotification *)aNotification
58 {
59     wxUnusedVar(aNotification);
60     wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
61     if ( impl && impl->ShouldSendEvents() )
62     {
63         wxWindow* wxpeer = (wxWindow*) impl->GetWXPeer();
64         if ( wxpeer ) {
65             wxCommandEvent event(wxEVT_TEXT, wxpeer->GetId());
66             event.SetEventObject( wxpeer );
67             event.SetString( static_cast<wxComboBox*>(wxpeer)->GetValue() );
68             wxpeer->HandleWindowEvent( event );
69         }
70     }
71 }
72
73 - (void)comboBoxSelectionDidChange:(NSNotification *)notification
74 {
75     wxUnusedVar(notification);
76     wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
77     if ( impl && impl->ShouldSendEvents())
78     {
79         wxComboBox* wxpeer = static_cast<wxComboBox*>(impl->GetWXPeer());
80         if ( wxpeer ) {
81             const int sel = wxpeer->GetSelection();
82
83             wxCommandEvent event(wxEVT_COMBOBOX, wxpeer->GetId());
84             event.SetEventObject( wxpeer );
85             event.SetInt( sel );
86             event.SetString( wxpeer->GetString(sel) );
87             // For some reason, wxComboBox::GetValue will not return the newly selected item 
88             // while we're inside this callback, so use AddPendingEvent to make sure
89             // GetValue() returns the right value.
90             wxEventLoop* const loop = (wxEventLoop*) wxEventLoopBase::GetActive();
91             if ( loop )
92                 loop->OSXUseLowLevelWakeup(true);
93
94             wxpeer->GetEventHandler()->AddPendingEvent( event );
95
96             if ( loop )
97                 loop->OSXUseLowLevelWakeup(false);
98         }
99     }
100 }
101 @end
102
103 wxNSComboBoxControl::wxNSComboBoxControl( wxComboBox *wxPeer, WXWidget w )
104     : wxNSTextFieldControl(wxPeer, wxPeer, w)
105 {
106     m_comboBox = (NSComboBox*)w;
107 }
108
109 wxNSComboBoxControl::~wxNSComboBoxControl()
110 {
111 }
112
113 int wxNSComboBoxControl::GetSelectedItem() const
114 {
115     return [m_comboBox indexOfSelectedItem];
116 }
117
118 void wxNSComboBoxControl::SetSelectedItem(int item)
119 {
120     SendEvents(false);
121
122     if ( item != wxNOT_FOUND )
123     {
124         wxASSERT_MSG( item >= 0 && item < [m_comboBox numberOfItems],
125                       "Inavlid item index." );
126         [m_comboBox selectItemAtIndex: item];
127     }
128     else // remove current selection (if we have any)
129     {
130         const int sel = GetSelectedItem();
131         if ( sel != wxNOT_FOUND )
132             [m_comboBox deselectItemAtIndex:sel];
133     }
134
135     SendEvents(true);
136 }
137
138 int wxNSComboBoxControl::GetNumberOfItems() const
139 {
140     return [m_comboBox numberOfItems];
141 }
142
143 void wxNSComboBoxControl::InsertItem(int pos, const wxString& item)
144 {
145     [m_comboBox insertItemWithObjectValue:wxCFStringRef( item , m_wxPeer->GetFont().GetEncoding() ).AsNSString() atIndex:pos];
146 }
147
148 void wxNSComboBoxControl::RemoveItem(int pos)
149 {
150     SendEvents(false);
151     [m_comboBox removeItemAtIndex:pos];
152     SendEvents(true);
153 }
154
155 void wxNSComboBoxControl::Clear()
156 {
157     SendEvents(false);
158     [m_comboBox removeAllItems];
159     SendEvents(true);
160 }
161
162 wxString wxNSComboBoxControl::GetStringAtIndex(int pos) const
163 {
164     return wxCFStringRef::AsString([m_comboBox itemObjectValueAtIndex:pos], m_wxPeer->GetFont().GetEncoding());
165 }
166
167 int wxNSComboBoxControl::FindString(const wxString& text) const
168 {
169     NSInteger nsresult = [m_comboBox indexOfItemWithObjectValue:wxCFStringRef( text , m_wxPeer->GetFont().GetEncoding() ).AsNSString()];
170
171     int result;
172     if (nsresult == NSNotFound)
173         result = wxNOT_FOUND;
174     else
175         result = (int) nsresult;
176     return result;
177 }
178
179 void wxNSComboBoxControl::Popup()
180 {
181     id ax = NSAccessibilityUnignoredDescendant(m_comboBox);
182     [ax accessibilitySetValue: [NSNumber numberWithBool: YES] forAttribute: NSAccessibilityExpandedAttribute];
183 }
184
185 void wxNSComboBoxControl::Dismiss()
186 {
187     id ax = NSAccessibilityUnignoredDescendant(m_comboBox);
188     [ax accessibilitySetValue: [NSNumber numberWithBool: NO] forAttribute: NSAccessibilityExpandedAttribute];
189 }
190
191 wxWidgetImplType* wxWidgetImpl::CreateComboBox( wxComboBox* wxpeer, 
192                                     wxWindowMac* WXUNUSED(parent), 
193                                     wxWindowID WXUNUSED(id), 
194                                     wxMenu* WXUNUSED(menu),
195                                     const wxPoint& pos, 
196                                     const wxSize& size,
197                                     long style, 
198                                     long WXUNUSED(extraStyle))
199 {
200     NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
201     wxNSComboBox* v = [[wxNSComboBox alloc] initWithFrame:r];
202     if (style & wxCB_READONLY)
203         [v setEditable:NO];
204     wxNSComboBoxControl* c = new wxNSComboBoxControl( wxpeer, v );
205     return c;
206 }
207
208 wxSize wxComboBox::DoGetBestSize() const
209 {
210     int lbWidth = GetCount() > 0 ? 20 : 100;  // some defaults
211     wxSize baseSize = wxWindow::DoGetBestSize();
212     int lbHeight = baseSize.y;
213     int wLine;
214     
215     {
216         wxClientDC dc(const_cast<wxComboBox*>(this));
217         
218         // Find the widest line
219         for(unsigned int i = 0; i < GetCount(); i++)
220         {
221             wxString str(GetString(i));
222             
223             wxCoord width, height ;
224             dc.GetTextExtent( str , &width, &height);
225             wLine = width ;
226             
227             lbWidth = wxMax( lbWidth, wLine ) ;
228         }
229         
230         // Add room for the popup arrow
231         lbWidth += 2 * lbHeight ;
232     }
233     
234     return wxSize( lbWidth, lbHeight );
235 }
236
237 #endif // wxUSE_COMBOBOX