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