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"
22 #include "wx/mac/uma.h"
24 extern MenuHandle
NewUniqueMenu() ;
26 IMPLEMENT_DYNAMIC_CLASS(wxChoice
, wxControl
)
31 if ( HasClientObjectData() )
33 unsigned int i
, max
= GetCount();
35 for ( i
= 0; i
< max
; ++i
)
36 delete GetClientObject( i
);
39 // DeleteMenu( m_macPopUpMenuId ) ;
40 // DisposeMenu( m_macPopUpMenuHandle ) ;
43 bool wxChoice::Create(wxWindow
*parent
,
47 const wxArrayString
& choices
,
49 const wxValidator
& validator
,
50 const wxString
& name
)
52 wxCArrayString
chs( choices
);
55 parent
, id
, pos
, size
, chs
.GetCount(), chs
.GetStrings(),
56 style
, validator
, name
);
59 bool wxChoice::Create(wxWindow
*parent
,
64 const wxString choices
[],
66 const wxValidator
& validator
,
67 const wxString
& name
)
69 m_macIsUserPane
= false;
71 if ( !wxChoiceBase::Create( parent
, id
, pos
, size
, style
, validator
, name
) )
74 Rect bounds
= wxMacGetBoundsForControl( this , pos
, size
);
76 m_peer
= new wxMacControl( this ) ;
77 OSStatus err
= CreatePopupButtonControl(
78 MAC_WXHWND(parent
->MacGetTopLevelWindowRef()) , &bounds
, CFSTR("") ,
79 -12345 , false /* no variable width */ , 0 , 0 , 0 , m_peer
->GetControlRefAddr() );
82 m_macPopUpMenuHandle
= NewUniqueMenu() ;
83 m_peer
->SetData
<MenuHandle
>( kControlNoPart
, kControlPopupButtonMenuHandleTag
, (MenuHandle
) m_macPopUpMenuHandle
) ;
84 m_peer
->SetValueAndRange( n
> 0 ? 1 : 0 , 0 , 0 );
85 MacPostControlCreate( pos
, size
);
88 if ( style
& wxCB_SORT
)
90 m_strings
= wxArrayString( 1 );
93 for ( int i
= 0; i
< n
; i
++ )
98 // Set the first item as being selected
102 // Needed because it is a wxControlWithItems
108 // ----------------------------------------------------------------------------
109 // adding/deleting items to/from the list
110 // ----------------------------------------------------------------------------
111 int wxChoice::DoAppend( const wxString
& item
)
114 wxArrayString::iterator insertPoint
;
117 if (GetWindowStyle() & wxCB_SORT
)
119 insertPoint
= std::lower_bound( m_strings
.begin(), m_strings
.end(), item
);
120 index
= insertPoint
- m_strings
.begin();
124 insertPoint
= m_strings
.end();
125 index
= m_strings
.size();
128 m_strings
.insert( insertPoint
, item
);
130 unsigned int index
= m_strings
.Add( item
);
133 m_datas
.Insert( NULL
, index
);
134 UMAInsertMenuItem( MAC_WXHMENU( m_macPopUpMenuHandle
), item
, m_font
.GetEncoding(), index
);
135 DoSetItemClientData( index
, NULL
);
136 m_peer
->SetMaximum( GetCount() );
141 int wxChoice::DoInsert( const wxString
& item
, unsigned int pos
)
143 wxCHECK_MSG( !(GetWindowStyle() & wxCB_SORT
), -1, wxT("wxChoice::DoInsert: can't insert into sorted list") );
144 wxCHECK_MSG( IsValidInsert(pos
), -1, wxT("wxChoice::DoInsert: invalid index") );
146 if (pos
== GetCount())
147 return DoAppend( item
);
149 UMAInsertMenuItem( MAC_WXHMENU( m_macPopUpMenuHandle
), item
, m_font
.GetEncoding(), pos
);
150 m_strings
.Insert( item
, pos
);
151 m_datas
.Insert( NULL
, pos
);
152 DoSetItemClientData( pos
, NULL
);
153 m_peer
->SetMaximum( GetCount() );
158 void wxChoice::Delete(unsigned int n
)
160 wxCHECK_RET( IsValid(n
) , wxT("wxChoice::Delete: invalid index") );
162 if ( HasClientObjectData() )
163 delete GetClientObject( n
);
165 ::DeleteMenuItem( MAC_WXHMENU(m_macPopUpMenuHandle
) , n
+ 1 ) ;
166 m_strings
.RemoveAt( n
) ;
167 m_datas
.RemoveAt( n
) ;
168 m_peer
->SetMaximum( GetCount() ) ;
171 void wxChoice::Clear()
174 for ( unsigned int i
= 0 ; i
< GetCount() ; i
++ )
176 ::DeleteMenuItem( MAC_WXHMENU(m_macPopUpMenuHandle
) , 1 ) ;
181 m_peer
->SetMaximum( 0 ) ;
184 void wxChoice::FreeData()
186 if ( HasClientObjectData() )
188 unsigned int count
= GetCount();
189 for ( unsigned int n
= 0; n
< count
; n
++ )
191 delete GetClientObject( n
);
196 // ----------------------------------------------------------------------------
198 // ----------------------------------------------------------------------------
199 int wxChoice::GetSelection() const
201 return m_peer
->GetValue() - 1 ;
204 void wxChoice::SetSelection( int n
)
206 m_peer
->SetValue( n
+ 1 ) ;
209 // ----------------------------------------------------------------------------
210 // string list functions
211 // ----------------------------------------------------------------------------
213 unsigned int wxChoice::GetCount() const
215 return m_strings
.GetCount() ;
218 int wxChoice::FindString( const wxString
& s
, bool bCase
) const
220 return m_strings
.Index( s
, bCase
) ;
223 void wxChoice::SetString(unsigned int n
, const wxString
& s
)
225 wxCHECK_RET( IsValid(n
), wxT("wxChoice::SetString(): invalid index") );
229 // apple menu pos is 1-based
230 UMASetMenuItemText( MAC_WXHMENU(m_macPopUpMenuHandle
) , n
+ 1 , s
, wxFont::GetDefaultEncoding() ) ;
233 wxString
wxChoice::GetString(unsigned int n
) const
235 wxCHECK_MSG( IsValid(n
), wxEmptyString
, wxT("wxChoice::GetString(): invalid index") );
237 return m_strings
[n
] ;
240 // ----------------------------------------------------------------------------
242 // ----------------------------------------------------------------------------
243 void wxChoice::DoSetItemClientData(unsigned int n
, void* clientData
)
245 wxCHECK_RET( IsValid(n
), wxT("wxChoice::DoSetItemClientData: invalid index") );
247 m_datas
[n
] = (char*)clientData
;
250 void * wxChoice::DoGetItemClientData(unsigned int n
) const
252 wxCHECK_MSG( IsValid(n
), NULL
, wxT("wxChoice::DoGetClientData: invalid index") );
254 return (void *)m_datas
[n
];
257 void wxChoice::DoSetItemClientObject(unsigned int n
, wxClientData
* clientData
)
259 DoSetItemClientData(n
, clientData
);
262 wxClientData
* wxChoice::DoGetItemClientObject(unsigned int n
) const
264 return (wxClientData
*)DoGetItemClientData( n
) ;
267 wxInt32
wxChoice::MacControlHit( WXEVENTHANDLERREF
WXUNUSED(handler
) , WXEVENTREF
WXUNUSED(event
) )
269 wxCommandEvent
event( wxEVT_COMMAND_CHOICE_SELECTED
, m_windowId
);
271 // actually n should be made sure by the os to be a valid selection, but ...
272 int n
= GetSelection();
276 event
.SetString( GetStringSelection() );
277 event
.SetEventObject( this );
279 if ( HasClientObjectData() )
280 event
.SetClientObject( GetClientObject( n
) );
281 else if ( HasClientUntypedData() )
282 event
.SetClientData( GetClientData( n
) );
284 ProcessCommand( event
);
290 wxSize
wxChoice::DoGetBestSize() const
292 int lbWidth
= GetCount() > 0 ? 20 : 100; // some defaults
299 GetThemeMetric( kThemeMetricPopupButtonHeight
, &metric
);
304 wxMacPortStateHelper
st( UMAGetWindowPort( (WindowRef
) MacGetTopLevelWindowRef() ) ) ;
307 ::TextFont( m_font
.MacGetFontNum() ) ;
308 ::TextSize( m_font
.MacGetFontSize() ) ;
309 ::TextFace( m_font
.MacGetFontStyle() ) ;
313 ::TextFont( kFontIDMonaco
) ;
318 // Find the widest line
319 for(unsigned int i
= 0; i
< GetCount(); i
++)
321 wxString
str(GetString(i
));
324 Point bounds
= { 0, 0 } ;
327 ::GetThemeTextDimensions( wxMacCFStringHolder( str
, m_font
.GetEncoding() ) ,
328 kThemeCurrentPortFont
,
336 wLine
= ::TextWidth( str
.c_str() , 0 , str
.length() ) ;
339 lbWidth
= wxMax( lbWidth
, wLine
) ;
342 // Add room for the popup arrow
343 lbWidth
+= 2 * lbHeight
;
345 // And just a bit more
346 int cx
= ::TextWidth( "X" , 0 , 1 ) ;
350 return wxSize( lbWidth
, lbHeight
);
353 #endif // wxUSE_CHOICE