]> git.saurik.com Git - wxWidgets.git/blame - src/osx/cocoa/combobox.mm
On OSX don't propogate the alignment setting from column to renderer if it is a custo...
[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
7// RCS-ID: $Id: combobox.mm 54129 2008-06-11 19:30:52Z 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
c072b9ec 168wxWidgetImplType* wxWidgetImpl::CreateComboBox( wxComboBox* wxpeer,
4ddfa282
SC
169 wxWindowMac* WXUNUSED(parent),
170 wxWindowID WXUNUSED(id),
171 wxMenu* menu,
172 const wxPoint& pos,
173 const wxSize& size,
ec073e73 174 long style,
4ddfa282
SC
175 long WXUNUSED(extraStyle))
176{
177 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
f941a30b 178 wxNSComboBox* v = [[wxNSComboBox alloc] initWithFrame:r];
ec073e73
KO
179 if (style & wxCB_READONLY)
180 [v setEditable:NO];
c84030e0 181 wxNSComboBoxControl* c = new wxNSComboBoxControl( wxpeer, v );
4ddfa282
SC
182 return c;
183}
184
5bd77105
SC
185wxSize wxComboBox::DoGetBestSize() const
186{
187 int lbWidth = GetCount() > 0 ? 20 : 100; // some defaults
188 wxSize baseSize = wxWindow::DoGetBestSize();
189 int lbHeight = baseSize.y;
190 int wLine;
191
192 {
193 wxClientDC dc(const_cast<wxComboBox*>(this));
194
195 // Find the widest line
196 for(unsigned int i = 0; i < GetCount(); i++)
197 {
198 wxString str(GetString(i));
199
200 wxCoord width, height ;
201 dc.GetTextExtent( str , &width, &height);
202 wLine = width ;
203
204 lbWidth = wxMax( lbWidth, wLine ) ;
205 }
206
207 // Add room for the popup arrow
208 lbWidth += 2 * lbHeight ;
209 }
210
211 return wxSize( lbWidth, lbHeight );
212}
213
c072b9ec 214#endif // wxUSE_COMBOBOX