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
221 // Avoid assert for non-default args passed to sorted array Index
222 if ( HasFlag(wxCB_SORT
) )
226 return m_strings
.Index( s
, bCase
) ;
229 void wxChoice::SetString(unsigned int n
, const wxString
& s
)
231 wxCHECK_RET( IsValid(n
), wxT("wxChoice::SetString(): invalid index") );
235 // apple menu pos is 1-based
236 UMASetMenuItemText( MAC_WXHMENU(m_macPopUpMenuHandle
) , n
+ 1 , s
, wxFont::GetDefaultEncoding() ) ;
239 wxString
wxChoice::GetString(unsigned int n
) const
241 wxCHECK_MSG( IsValid(n
), wxEmptyString
, wxT("wxChoice::GetString(): invalid index") );
243 return m_strings
[n
] ;
246 // ----------------------------------------------------------------------------
248 // ----------------------------------------------------------------------------
249 void wxChoice::DoSetItemClientData(unsigned int n
, void* clientData
)
251 wxCHECK_RET( IsValid(n
), wxT("wxChoice::DoSetItemClientData: invalid index") );
253 m_datas
[n
] = (char*)clientData
;
256 void * wxChoice::DoGetItemClientData(unsigned int n
) const
258 wxCHECK_MSG( IsValid(n
), NULL
, wxT("wxChoice::DoGetClientData: invalid index") );
260 return (void *)m_datas
[n
];
263 void wxChoice::DoSetItemClientObject(unsigned int n
, wxClientData
* clientData
)
265 DoSetItemClientData(n
, clientData
);
268 wxClientData
* wxChoice::DoGetItemClientObject(unsigned int n
) const
270 return (wxClientData
*)DoGetItemClientData( n
) ;
273 wxInt32
wxChoice::MacControlHit( WXEVENTHANDLERREF
WXUNUSED(handler
) , WXEVENTREF
WXUNUSED(event
) )
275 wxCommandEvent
event( wxEVT_COMMAND_CHOICE_SELECTED
, m_windowId
);
277 // actually n should be made sure by the os to be a valid selection, but ...
278 int n
= GetSelection();
282 event
.SetString( GetStringSelection() );
283 event
.SetEventObject( this );
285 if ( HasClientObjectData() )
286 event
.SetClientObject( GetClientObject( n
) );
287 else if ( HasClientUntypedData() )
288 event
.SetClientData( GetClientData( n
) );
290 ProcessCommand( event
);
296 wxSize
wxChoice::DoGetBestSize() const
298 int lbWidth
= GetCount() > 0 ? 20 : 100; // some defaults
305 GetThemeMetric( kThemeMetricPopupButtonHeight
, &metric
);
310 wxMacPortStateHelper
st( UMAGetWindowPort( (WindowRef
) MacGetTopLevelWindowRef() ) ) ;
313 ::TextFont( m_font
.MacGetFontNum() ) ;
314 ::TextSize( m_font
.MacGetFontSize() ) ;
315 ::TextFace( m_font
.MacGetFontStyle() ) ;
319 ::TextFont( kFontIDMonaco
) ;
324 // Find the widest line
325 for(unsigned int i
= 0; i
< GetCount(); i
++)
327 wxString
str(GetString(i
));
330 Point bounds
= { 0, 0 } ;
333 ::GetThemeTextDimensions( wxMacCFStringHolder( str
, m_font
.GetEncoding() ) ,
334 kThemeCurrentPortFont
,
342 wLine
= ::TextWidth( str
.c_str() , 0 , str
.length() ) ;
345 lbWidth
= wxMax( lbWidth
, wLine
) ;
348 // Add room for the popup arrow
349 lbWidth
+= 2 * lbHeight
;
351 // And just a bit more
352 int cx
= ::TextWidth( "X" , 0 , 1 ) ;
356 return wxSize( lbWidth
, lbHeight
);
359 #endif // wxUSE_CHOICE