1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Stefan Csomor
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
13 #pragma implementation "choice.h"
16 #include "wx/wxprec.h"
20 #include "wx/choice.h"
22 #include "wx/mac/uma.h"
24 IMPLEMENT_DYNAMIC_CLASS(wxChoice
, wxControl
)
26 extern MenuHandle
NewUniqueMenu() ;
30 if ( HasClientObjectData() )
32 size_t i
, max
= GetCount();
34 for ( i
= 0; i
< max
; ++i
)
35 delete GetClientObject(i
);
38 // DeleteMenu( m_macPopUpMenuId ) ;
39 // DisposeMenu( m_macPopUpMenuHandle ) ;
42 bool wxChoice::Create(wxWindow
*parent
, wxWindowID id
,
45 const wxArrayString
& choices
,
47 const wxValidator
& validator
,
50 wxCArrayString
chs(choices
);
52 return Create(parent
, id
, pos
, size
, chs
.GetCount(), chs
.GetStrings(),
53 style
, validator
, name
);
56 bool wxChoice::Create(wxWindow
*parent
, wxWindowID id
,
59 int n
, const wxString choices
[],
61 const wxValidator
& validator
,
64 m_macIsUserPane
= false ;
66 if ( !wxChoiceBase::Create(parent
, id
, pos
, size
, style
, validator
, name
) )
69 Rect bounds
= wxMacGetBoundsForControl( this , pos
, size
) ;
71 m_peer
= new wxMacControl(this) ;
72 verify_noerr ( CreatePopupButtonControl( MAC_WXHWND(parent
->MacGetTopLevelWindowRef()) , &bounds
, CFSTR("") ,
73 -12345 , false /* no variable width */ , 0 , 0 , 0 , m_peer
->GetControlRefAddr() ) );
76 m_macPopUpMenuHandle
= NewUniqueMenu() ;
77 m_peer
->SetData
<MenuHandle
>( kControlNoPart
, kControlPopupButtonMenuHandleTag
, (MenuHandle
) m_macPopUpMenuHandle
) ;
78 m_peer
->SetValueAndRange( n
> 0 ? 1 : 0 , 0 , 0 ) ;
79 MacPostControlCreate(pos
,size
) ;
82 if ( style
& wxCB_SORT
)
84 m_strings
= wxArrayString(1) ; // autosort
88 for ( int i
= 0; i
< n
; i
++ )
92 SetBestSize(size
); // Needed because it is a wxControlWithItems
96 // ----------------------------------------------------------------------------
97 // adding/deleting items to/from the list
98 // ----------------------------------------------------------------------------
99 int wxChoice::DoAppend(const wxString
& item
)
102 wxArrayString::iterator insertPoint
;
105 if (GetWindowStyle() & wxCB_SORT
)
107 insertPoint
= std::lower_bound( m_strings
.begin(), m_strings
.end(), item
);
108 index
= insertPoint
- m_strings
.begin();
112 insertPoint
= m_strings
.end();
113 index
= m_strings
.size();
116 m_strings
.insert( insertPoint
, item
);
118 size_t index
= m_strings
.Add( item
) ;
120 m_datas
.Insert( NULL
, index
) ;
121 UMAInsertMenuItem(MAC_WXHMENU( m_macPopUpMenuHandle
) , item
, m_font
.GetEncoding() , index
);
122 DoSetItemClientData( index
, NULL
) ;
123 m_peer
->SetMaximum( GetCount() ) ;
127 int wxChoice::DoInsert(const wxString
& item
, int pos
)
129 wxCHECK_MSG(!(GetWindowStyle() & wxCB_SORT
), -1, wxT("can't insert into sorted list"));
130 wxCHECK_MSG((pos
>=0) && (pos
<=GetCount()), -1, wxT("invalid index"));
132 if (pos
== GetCount())
133 return DoAppend(item
);
135 UMAInsertMenuItem(MAC_WXHMENU( m_macPopUpMenuHandle
) , item
, m_font
.GetEncoding() , pos
);
136 m_strings
.Insert( item
, pos
) ;
137 m_datas
.Insert( NULL
, pos
) ;
138 DoSetItemClientData( pos
, NULL
) ;
139 m_peer
->SetMaximum( GetCount() ) ;
143 void wxChoice::Delete(int n
)
145 wxCHECK_RET( n
< GetCount(), wxT("invalid item index in wxChoice::Delete") );
146 if ( HasClientObjectData() )
148 delete GetClientObject(n
);
150 ::DeleteMenuItem( MAC_WXHMENU(m_macPopUpMenuHandle
) , n
+ 1) ;
151 m_strings
.RemoveAt( n
) ;
152 m_datas
.RemoveAt( n
) ;
153 m_peer
->SetMaximum( GetCount() ) ;
156 void wxChoice::Clear()
159 for ( int i
= 0 ; i
< GetCount() ; i
++ )
161 ::DeleteMenuItem( MAC_WXHMENU(m_macPopUpMenuHandle
) , 1 ) ;
165 m_peer
->SetMaximum( 0 ) ;
168 void wxChoice::FreeData()
170 if ( HasClientObjectData() )
172 size_t count
= GetCount();
173 for ( size_t n
= 0; n
< count
; n
++ )
175 delete GetClientObject(n
);
180 // ----------------------------------------------------------------------------
182 // ----------------------------------------------------------------------------
183 int wxChoice::GetSelection() const
185 return m_peer
->GetValue() -1 ;
188 void wxChoice::SetSelection(int n
)
190 m_peer
->SetValue( n
+ 1 ) ;
193 // ----------------------------------------------------------------------------
194 // string list functions
195 // ----------------------------------------------------------------------------
197 int wxChoice::GetCount() const
199 return m_strings
.GetCount() ;
202 int wxChoice::FindString(const wxString
& s
) const
204 return m_strings
.Index( s
, true , false) ;
207 void wxChoice::SetString(int n
, const wxString
& s
)
210 // apple menu pos is 1-based
211 UMASetMenuItemText( MAC_WXHMENU(m_macPopUpMenuHandle
) , n
+ 1 , s
, wxFont::GetDefaultEncoding() ) ;
214 wxString
wxChoice::GetString(int n
) const
216 wxCHECK_MSG( n
>= 0 && (size_t)n
< m_strings
.GetCount(), _T(""),
217 _T("wxChoice::GetString(): invalid index") );
219 return m_strings
[n
] ;
222 // ----------------------------------------------------------------------------
224 // ----------------------------------------------------------------------------
225 void wxChoice::DoSetItemClientData( int n
, void* clientData
)
227 wxCHECK_RET( n
>= 0 && (size_t)n
< m_datas
.GetCount(),
228 wxT("invalid index in wxChoice::SetClientData") );
230 m_datas
[n
] = (char*) clientData
;
233 void *wxChoice::DoGetItemClientData(int n
) const
235 wxCHECK_MSG( n
>= 0 && (size_t)n
< m_datas
.GetCount(), NULL
,
236 wxT("invalid index in wxChoice::GetClientData") );
237 return (void *)m_datas
[n
];
240 void wxChoice::DoSetItemClientObject( int n
, wxClientData
* clientData
)
242 DoSetItemClientData(n
, clientData
);
245 wxClientData
* wxChoice::DoGetItemClientObject( int n
) const
247 return (wxClientData
*)DoGetItemClientData(n
);
250 wxInt32
wxChoice::MacControlHit(WXEVENTHANDLERREF
WXUNUSED(handler
) , WXEVENTREF
WXUNUSED(event
) )
252 wxCommandEvent
event(wxEVT_COMMAND_CHOICE_SELECTED
, m_windowId
);
253 int n
= GetSelection();
254 // actually n should be made sure by the os to be a valid selection, but ...
258 event
.SetString(GetStringSelection());
259 event
.SetEventObject(this);
260 if ( HasClientObjectData() )
261 event
.SetClientObject( GetClientObject(n
) );
262 else if ( HasClientUntypedData() )
263 event
.SetClientData( GetClientData(n
) );
264 ProcessCommand(event
);
269 wxSize
wxChoice::DoGetBestSize() const
271 int lbWidth
= GetCount() > 0 ? 20 : 100; // some defaults
276 GetThemeMetric(kThemeMetricPopupButtonHeight
, &metric
);
280 wxMacPortStateHelper
st( UMAGetWindowPort( (WindowRef
) MacGetTopLevelWindowRef() ) ) ;
283 ::TextFont( m_font
.MacGetFontNum() ) ;
284 ::TextSize( m_font
.MacGetFontSize() ) ;
285 ::TextFace( m_font
.MacGetFontStyle() ) ;
289 ::TextFont( kFontIDMonaco
) ;
293 // Find the widest line
294 for(int i
= 0; i
< GetCount(); i
++) {
295 wxString
str(GetString(i
));
299 ::GetThemeTextDimensions( wxMacCFStringHolder( str
, m_font
.GetEncoding() ) ,
300 kThemeCurrentPortFont
,
307 wLine
= ::TextWidth( str
.c_str() , 0 , str
.Length() ) ;
309 lbWidth
= wxMax(lbWidth
, wLine
);
311 // Add room for the popup arrow
312 lbWidth
+= 2 * lbHeight
;
313 // And just a bit more
314 int cx
= ::TextWidth( "X" , 0 , 1 ) ;
318 return wxSize(lbWidth
, lbHeight
);
321 #endif // wxUSE_CHOICE