]> git.saurik.com Git - wxWidgets.git/blame - src/osx/cocoa/combobox.mm
Add the possibility to disable invisible wxDataViewCtrl items.
[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
27@interface wxNSComboBox : NSComboBox
28{
29}
30
31@end
32
33@implementation wxNSComboBox
34
35+ (void)initialize
36{
37 static BOOL initialized = NO;
38 if (!initialized)
39 {
40 initialized = YES;
41 wxOSXCocoaClassAddWXMethods( self );
42 }
43}
44
c84030e0 45- (void)controlTextDidChange:(NSNotification *)aNotification
4ddfa282 46{
c84030e0
KO
47 wxUnusedVar(aNotification);
48 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
809020fc 49 if ( impl && impl->ShouldSendEvents() )
c84030e0
KO
50 {
51 wxWindow* wxpeer = (wxWindow*) impl->GetWXPeer();
52 if ( wxpeer ) {
53 wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, wxpeer->GetId());
54 event.SetEventObject( wxpeer );
55 event.SetString( static_cast<wxComboBox*>(wxpeer)->GetValue() );
56 wxpeer->HandleWindowEvent( event );
57 }
58 }
4ddfa282
SC
59}
60
c84030e0 61- (void)comboBoxSelectionDidChange:(NSNotification *)notification
4ddfa282 62{
c84030e0
KO
63 wxUnusedVar(notification);
64 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
809020fc 65 if ( impl && impl->ShouldSendEvents())
c84030e0
KO
66 {
67 wxWindow* wxpeer = (wxWindow*) impl->GetWXPeer();
68 if ( wxpeer ) {
69 wxCommandEvent event(wxEVT_COMMAND_COMBOBOX_SELECTED, wxpeer->GetId());
70 event.SetEventObject( wxpeer );
71 event.SetInt( static_cast<wxComboBox*>(wxpeer)->GetSelection() );
72 // For some reason, wxComboBox::GetValue will not return the newly selected item
73 // while we're inside this callback, so use AddPendingEvent to make sure
74 // GetValue() returns the right value.
75 wxpeer->GetEventHandler()->AddPendingEvent( event );
76 }
77 }
4ddfa282 78}
4ddfa282
SC
79@end
80
c072b9ec
VZ
81wxNSComboBoxControl::wxNSComboBoxControl( wxComboBox *wxPeer, WXWidget w )
82 : wxNSTextFieldControl(wxPeer, wxPeer, w)
c84030e0
KO
83{
84 m_comboBox = (NSComboBox*)w;
85}
86
87wxNSComboBoxControl::~wxNSComboBoxControl()
88{
89}
90
91int wxNSComboBoxControl::GetSelectedItem() const
92{
93 return [m_comboBox indexOfSelectedItem];
94}
95
96void wxNSComboBoxControl::SetSelectedItem(int item)
97{
809020fc 98 SendEvents(false);
6f07c007
VZ
99
100 if ( item != wxNOT_FOUND )
101 {
102 wxASSERT_MSG( item >= 0 && item < [m_comboBox numberOfItems],
103 "Inavlid item index." );
104 [m_comboBox selectItemAtIndex: item];
105 }
106 else // remove current selection (if we have any)
107 {
108 const int sel = GetSelectedItem();
109 if ( sel != wxNOT_FOUND )
110 [m_comboBox deselectItemAtIndex:sel];
111 }
112
809020fc 113 SendEvents(true);
c84030e0
KO
114}
115
116int wxNSComboBoxControl::GetNumberOfItems() const
117{
118 return [m_comboBox numberOfItems];
119}
120
121void wxNSComboBoxControl::InsertItem(int pos, const wxString& item)
122{
123 [m_comboBox insertItemWithObjectValue:wxCFStringRef( item , m_wxPeer->GetFont().GetEncoding() ).AsNSString() atIndex:pos];
124}
125
126void wxNSComboBoxControl::RemoveItem(int pos)
127{
809020fc 128 SendEvents(false);
c84030e0 129 [m_comboBox removeItemAtIndex:pos];
809020fc 130 SendEvents(true);
c84030e0
KO
131}
132
133void wxNSComboBoxControl::Clear()
134{
809020fc 135 SendEvents(false);
c84030e0 136 [m_comboBox removeAllItems];
809020fc 137 SendEvents(true);
c84030e0
KO
138}
139
140wxString wxNSComboBoxControl::GetStringAtIndex(int pos) const
141{
142 return wxCFStringRef::AsString([m_comboBox itemObjectValueAtIndex:pos], m_wxPeer->GetFont().GetEncoding());
143}
144
145int wxNSComboBoxControl::FindString(const wxString& text) const
146{
79323592
SC
147 NSInteger nsresult = [m_comboBox indexOfItemWithObjectValue:wxCFStringRef( text , m_wxPeer->GetFont().GetEncoding() ).AsNSString()];
148
149 int result;
150 if (nsresult == NSNotFound)
2c755d9b 151 result = wxNOT_FOUND;
79323592
SC
152 else
153 result = (int) nsresult;
2c755d9b 154 return result;
c84030e0
KO
155}
156
c072b9ec 157wxWidgetImplType* wxWidgetImpl::CreateComboBox( wxComboBox* wxpeer,
4ddfa282
SC
158 wxWindowMac* WXUNUSED(parent),
159 wxWindowID WXUNUSED(id),
160 wxMenu* menu,
161 const wxPoint& pos,
162 const wxSize& size,
ec073e73 163 long style,
4ddfa282
SC
164 long WXUNUSED(extraStyle))
165{
166 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
f941a30b 167 wxNSComboBox* v = [[wxNSComboBox alloc] initWithFrame:r];
ec073e73
KO
168 if (style & wxCB_READONLY)
169 [v setEditable:NO];
c84030e0 170 wxNSComboBoxControl* c = new wxNSComboBoxControl( wxpeer, v );
4ddfa282
SC
171 return c;
172}
173
5bd77105
SC
174wxSize wxComboBox::DoGetBestSize() const
175{
176 int lbWidth = GetCount() > 0 ? 20 : 100; // some defaults
177 wxSize baseSize = wxWindow::DoGetBestSize();
178 int lbHeight = baseSize.y;
179 int wLine;
180
181 {
182 wxClientDC dc(const_cast<wxComboBox*>(this));
183
184 // Find the widest line
185 for(unsigned int i = 0; i < GetCount(); i++)
186 {
187 wxString str(GetString(i));
188
189 wxCoord width, height ;
190 dc.GetTextExtent( str , &width, &height);
191 wLine = width ;
192
193 lbWidth = wxMax( lbWidth, wLine ) ;
194 }
195
196 // Add room for the popup arrow
197 lbWidth += 2 * lbHeight ;
198 }
199
200 return wxSize( lbWidth, lbHeight );
201}
202
c072b9ec 203#endif // wxUSE_COMBOBOX