1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/mac/carbon/choice.cpp
4 // Author: Stefan Csomor
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
16 #include "wx/choice.h"
18 #include "wx/mac/uma.h"
20 extern MenuHandle
NewUniqueMenu() ;
22 IMPLEMENT_DYNAMIC_CLASS(wxChoice
, wxControl
)
27 if ( HasClientObjectData() )
29 unsigned int i
, max
= GetCount();
31 for ( i
= 0; i
< max
; ++i
)
32 delete GetClientObject( i
);
35 // DeleteMenu( m_macPopUpMenuId ) ;
36 // DisposeMenu( m_macPopUpMenuHandle ) ;
39 bool wxChoice::Create(wxWindow
*parent
,
43 const wxArrayString
& choices
,
45 const wxValidator
& validator
,
46 const wxString
& name
)
48 wxCArrayString
chs( choices
);
51 parent
, id
, pos
, size
, chs
.GetCount(), chs
.GetStrings(),
52 style
, validator
, name
);
55 bool wxChoice::Create(wxWindow
*parent
,
60 const wxString choices
[],
62 const wxValidator
& validator
,
63 const wxString
& name
)
65 m_macIsUserPane
= false;
67 if ( !wxChoiceBase::Create( parent
, id
, pos
, size
, style
, validator
, name
) )
70 Rect bounds
= wxMacGetBoundsForControl( this , pos
, size
);
72 m_peer
= new wxMacControl( this ) ;
73 OSStatus err
= CreatePopupButtonControl(
74 MAC_WXHWND(parent
->MacGetTopLevelWindowRef()) , &bounds
, CFSTR("") ,
75 -12345 , false /* no variable width */ , 0 , 0 , 0 , m_peer
->GetControlRefAddr() );
78 m_macPopUpMenuHandle
= NewUniqueMenu() ;
79 m_peer
->SetData
<MenuHandle
>( kControlNoPart
, kControlPopupButtonMenuHandleTag
, (MenuHandle
) m_macPopUpMenuHandle
) ;
80 m_peer
->SetValueAndRange( n
> 0 ? 1 : 0 , 0 , 0 );
81 MacPostControlCreate( pos
, size
);
84 if ( style
& wxCB_SORT
)
86 m_strings
= wxArrayString( 1 );
89 for ( int i
= 0; i
< n
; i
++ )
94 // Set the first item as being selected
98 // Needed because it is a wxControlWithItems
104 // ----------------------------------------------------------------------------
105 // adding/deleting items to/from the list
106 // ----------------------------------------------------------------------------
107 int wxChoice::DoAppend( const wxString
& item
)
110 wxArrayString::iterator insertPoint
;
113 if (GetWindowStyle() & wxCB_SORT
)
115 insertPoint
= std::lower_bound( m_strings
.begin(), m_strings
.end(), item
);
116 index
= insertPoint
- m_strings
.begin();
120 insertPoint
= m_strings
.end();
121 index
= m_strings
.size();
124 m_strings
.insert( insertPoint
, item
);
126 unsigned int index
= m_strings
.Add( item
);
129 m_datas
.Insert( NULL
, index
);
130 UMAInsertMenuItem( MAC_WXHMENU( m_macPopUpMenuHandle
), item
, m_font
.GetEncoding(), index
);
131 DoSetItemClientData( index
, NULL
);
132 m_peer
->SetMaximum( GetCount() );
137 int wxChoice::DoInsert( const wxString
& item
, unsigned int pos
)
139 wxCHECK_MSG( !(GetWindowStyle() & wxCB_SORT
), -1, wxT("wxChoice::DoInsert: can't insert into sorted list") );
140 wxCHECK_MSG( IsValidInsert(pos
), -1, wxT("wxChoice::DoInsert: invalid index") );
142 if (pos
== GetCount())
143 return DoAppend( item
);
145 UMAInsertMenuItem( MAC_WXHMENU( m_macPopUpMenuHandle
), item
, m_font
.GetEncoding(), pos
);
146 m_strings
.Insert( item
, pos
);
147 m_datas
.Insert( NULL
, pos
);
148 DoSetItemClientData( pos
, NULL
);
149 m_peer
->SetMaximum( GetCount() );
154 void wxChoice::Delete(unsigned int n
)
156 wxCHECK_RET( IsValid(n
) , wxT("wxChoice::Delete: invalid index") );
158 if ( HasClientObjectData() )
159 delete GetClientObject( n
);
161 ::DeleteMenuItem( MAC_WXHMENU(m_macPopUpMenuHandle
) , n
+ 1 ) ;
162 m_strings
.RemoveAt( n
) ;
163 m_datas
.RemoveAt( n
) ;
164 m_peer
->SetMaximum( GetCount() ) ;
167 void wxChoice::Clear()
170 for ( unsigned int i
= 0 ; i
< GetCount() ; i
++ )
172 ::DeleteMenuItem( MAC_WXHMENU(m_macPopUpMenuHandle
) , 1 ) ;
177 m_peer
->SetMaximum( 0 ) ;
180 void wxChoice::FreeData()
182 if ( HasClientObjectData() )
184 unsigned int count
= GetCount();
185 for ( unsigned int n
= 0; n
< count
; n
++ )
187 delete GetClientObject( n
);
192 // ----------------------------------------------------------------------------
194 // ----------------------------------------------------------------------------
195 int wxChoice::GetSelection() const
197 return m_peer
->GetValue() - 1 ;
200 void wxChoice::SetSelection( int n
)
202 m_peer
->SetValue( n
+ 1 ) ;
205 // ----------------------------------------------------------------------------
206 // string list functions
207 // ----------------------------------------------------------------------------
209 unsigned int wxChoice::GetCount() const
211 return m_strings
.GetCount() ;
214 int wxChoice::FindString( const wxString
& s
, bool bCase
) const
216 return m_strings
.Index( s
, bCase
) ;
219 void wxChoice::SetString(unsigned int n
, const wxString
& s
)
221 wxCHECK_RET( IsValid(n
), wxT("wxChoice::SetString(): invalid index") );
225 // apple menu pos is 1-based
226 UMASetMenuItemText( MAC_WXHMENU(m_macPopUpMenuHandle
) , n
+ 1 , s
, wxFont::GetDefaultEncoding() ) ;
229 wxString
wxChoice::GetString(unsigned int n
) const
231 wxCHECK_MSG( IsValid(n
), wxEmptyString
, wxT("wxChoice::GetString(): invalid index") );
233 return m_strings
[n
] ;
236 // ----------------------------------------------------------------------------
238 // ----------------------------------------------------------------------------
239 void wxChoice::DoSetItemClientData(unsigned int n
, void* clientData
)
241 wxCHECK_RET( IsValid(n
), wxT("wxChoice::DoSetItemClientData: invalid index") );
243 m_datas
[n
] = (char*)clientData
;
246 void * wxChoice::DoGetItemClientData(unsigned int n
) const
248 wxCHECK_MSG( IsValid(n
), NULL
, wxT("wxChoice::DoGetClientData: invalid index") );
250 return (void *)m_datas
[n
];
253 void wxChoice::DoSetItemClientObject(unsigned int n
, wxClientData
* clientData
)
255 DoSetItemClientData(n
, clientData
);
258 wxClientData
* wxChoice::DoGetItemClientObject(unsigned int n
) const
260 return (wxClientData
*)DoGetItemClientData( n
) ;
263 wxInt32
wxChoice::MacControlHit( WXEVENTHANDLERREF
WXUNUSED(handler
) , WXEVENTREF
WXUNUSED(event
) )
265 wxCommandEvent
event( wxEVT_COMMAND_CHOICE_SELECTED
, m_windowId
);
267 // actually n should be made sure by the os to be a valid selection, but ...
268 int n
= GetSelection();
272 event
.SetString( GetStringSelection() );
273 event
.SetEventObject( this );
275 if ( HasClientObjectData() )
276 event
.SetClientObject( GetClientObject( n
) );
277 else if ( HasClientUntypedData() )
278 event
.SetClientData( GetClientData( n
) );
280 ProcessCommand( event
);
286 wxSize
wxChoice::DoGetBestSize() const
288 int lbWidth
= GetCount() > 0 ? 20 : 100; // some defaults
295 GetThemeMetric( kThemeMetricPopupButtonHeight
, &metric
);
300 wxMacPortStateHelper
st( UMAGetWindowPort( (WindowRef
) MacGetTopLevelWindowRef() ) ) ;
303 ::TextFont( m_font
.MacGetFontNum() ) ;
304 ::TextSize( m_font
.MacGetFontSize() ) ;
305 ::TextFace( m_font
.MacGetFontStyle() ) ;
309 ::TextFont( kFontIDMonaco
) ;
314 // Find the widest line
315 for(unsigned int i
= 0; i
< GetCount(); i
++)
317 wxString
str(GetString(i
));
320 Point bounds
= { 0, 0 } ;
323 ::GetThemeTextDimensions( wxMacCFStringHolder( str
, m_font
.GetEncoding() ) ,
324 kThemeCurrentPortFont
,
332 wLine
= ::TextWidth( str
.c_str() , 0 , str
.length() ) ;
335 lbWidth
= wxMax( lbWidth
, wLine
) ;
338 // Add room for the popup arrow
339 lbWidth
+= 2 * lbHeight
;
341 // And just a bit more
342 int cx
= ::TextWidth( "X" , 0 , 1 ) ;
346 return wxSize( lbWidth
, lbHeight
);
349 #endif // wxUSE_CHOICE