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 #if !USE_SHARED_LIBRARY
25 IMPLEMENT_DYNAMIC_CLASS(wxChoice
, wxControl
)
28 extern MenuHandle
NewUniqueMenu() ;
32 if ( HasClientObjectData() )
34 size_t i
, max
= GetCount();
36 for ( i
= 0; i
< max
; ++i
)
37 delete GetClientObject(i
);
40 // DeleteMenu( m_macPopUpMenuId ) ;
41 // DisposeMenu( m_macPopUpMenuHandle ) ;
44 bool wxChoice::Create(wxWindow
*parent
, wxWindowID id
,
47 const wxArrayString
& choices
,
49 const wxValidator
& validator
,
52 wxCArrayString
chs(choices
);
54 return Create(parent
, id
, pos
, size
, chs
.GetCount(), chs
.GetStrings(),
55 style
, validator
, name
);
58 bool wxChoice::Create(wxWindow
*parent
, wxWindowID id
,
61 int n
, const wxString choices
[],
63 const wxValidator
& validator
,
66 m_macIsUserPane
= false ;
68 if ( !wxChoiceBase::Create(parent
, id
, pos
, size
, style
, validator
, name
) )
71 Rect bounds
= wxMacGetBoundsForControl( this , pos
, size
) ;
73 m_peer
= new wxMacControl(this) ;
74 verify_noerr ( CreatePopupButtonControl( 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
) ;
83 // FIXME: STL version of wxArrayString doesn't have the same args
85 if ( style
& wxCB_SORT
)
87 m_strings
= wxArrayString(1) ; // autosort
91 for ( int i
= 0; i
< n
; i
++ )
95 SetBestSize(size
); // Needed because it is a wxControlWithItems
99 // ----------------------------------------------------------------------------
100 // adding/deleting items to/from the list
101 // ----------------------------------------------------------------------------
102 int wxChoice::DoAppend(const wxString
& item
)
104 // FIXME: STL version of wxArrayString doesn't have the same args
106 size_t index
= m_strings
.size();
107 m_strings
.Add( item
);
109 size_t index
= m_strings
.Add( item
) ;
111 m_datas
.Insert( NULL
, index
) ;
112 UMAInsertMenuItem(MAC_WXHMENU( m_macPopUpMenuHandle
) , item
, m_font
.GetEncoding() , index
);
113 DoSetItemClientData( index
, NULL
) ;
114 m_peer
->SetMaximum( GetCount() ) ;
118 int wxChoice::DoInsert(const wxString
& item
, int pos
)
120 wxCHECK_MSG(!(GetWindowStyle() & wxCB_SORT
), -1, wxT("can't insert into sorted list"));
121 wxCHECK_MSG((pos
>=0) && (pos
<=GetCount()), -1, wxT("invalid index"));
123 if (pos
== GetCount())
124 return DoAppend(item
);
126 UMAInsertMenuItem(MAC_WXHMENU( m_macPopUpMenuHandle
) , item
, m_font
.GetEncoding() , pos
);
127 m_strings
.Insert( item
, pos
) ;
128 m_datas
.Insert( NULL
, pos
) ;
129 DoSetItemClientData( pos
, NULL
) ;
130 m_peer
->SetMaximum( GetCount() ) ;
134 void wxChoice::Delete(int n
)
136 wxCHECK_RET( n
< GetCount(), wxT("invalid item index in wxChoice::Delete") );
137 if ( HasClientObjectData() )
139 delete GetClientObject(n
);
141 ::DeleteMenuItem( MAC_WXHMENU(m_macPopUpMenuHandle
) , n
+ 1) ;
142 m_strings
.RemoveAt( n
) ;
143 m_datas
.RemoveAt( n
) ;
144 m_peer
->SetMaximum( GetCount() ) ;
147 void wxChoice::Clear()
150 for ( int i
= 0 ; i
< GetCount() ; i
++ )
152 ::DeleteMenuItem( MAC_WXHMENU(m_macPopUpMenuHandle
) , 1 ) ;
156 m_peer
->SetMaximum( 0 ) ;
159 void wxChoice::FreeData()
161 if ( HasClientObjectData() )
163 size_t count
= GetCount();
164 for ( size_t n
= 0; n
< count
; n
++ )
166 delete GetClientObject(n
);
171 // ----------------------------------------------------------------------------
173 // ----------------------------------------------------------------------------
174 int wxChoice::GetSelection() const
176 return m_peer
->GetValue() -1 ;
179 void wxChoice::SetSelection(int n
)
181 m_peer
->SetValue( n
+ 1 ) ;
184 // ----------------------------------------------------------------------------
185 // string list functions
186 // ----------------------------------------------------------------------------
188 int wxChoice::GetCount() const
190 return m_strings
.GetCount() ;
193 int wxChoice::FindString(const wxString
& s
) const
195 return m_strings
.Index( s
, true , false) ;
198 void wxChoice::SetString(int n
, const wxString
& s
)
201 // apple menu pos is 1-based
202 UMASetMenuItemText( MAC_WXHMENU(m_macPopUpMenuHandle
) , n
+ 1 , s
, wxFont::GetDefaultEncoding() ) ;
205 wxString
wxChoice::GetString(int n
) const
207 wxCHECK_MSG( n
>= 0 && (size_t)n
< m_strings
.GetCount(), _T(""),
208 _T("wxChoice::GetString(): invalid index") );
210 return m_strings
[n
] ;
213 // ----------------------------------------------------------------------------
215 // ----------------------------------------------------------------------------
216 void wxChoice::DoSetItemClientData( int n
, void* clientData
)
218 wxCHECK_RET( n
>= 0 && (size_t)n
< m_datas
.GetCount(),
219 wxT("invalid index in wxChoice::SetClientData") );
221 m_datas
[n
] = (char*) clientData
;
224 void *wxChoice::DoGetItemClientData(int n
) const
226 wxCHECK_MSG( n
>= 0 && (size_t)n
< m_datas
.GetCount(), NULL
,
227 wxT("invalid index in wxChoice::GetClientData") );
228 return (void *)m_datas
[n
];
231 void wxChoice::DoSetItemClientObject( int n
, wxClientData
* clientData
)
233 DoSetItemClientData(n
, clientData
);
236 wxClientData
* wxChoice::DoGetItemClientObject( int n
) const
238 return (wxClientData
*)DoGetItemClientData(n
);
241 wxInt32
wxChoice::MacControlHit(WXEVENTHANDLERREF
WXUNUSED(handler
) , WXEVENTREF
WXUNUSED(event
) )
243 wxCommandEvent
event(wxEVT_COMMAND_CHOICE_SELECTED
, m_windowId
);
244 int n
= GetSelection();
245 // actually n should be made sure by the os to be a valid selection, but ...
249 event
.SetString(GetStringSelection());
250 event
.SetEventObject(this);
251 if ( HasClientObjectData() )
252 event
.SetClientObject( GetClientObject(n
) );
253 else if ( HasClientUntypedData() )
254 event
.SetClientData( GetClientData(n
) );
255 ProcessCommand(event
);
260 wxSize
wxChoice::DoGetBestSize() const
262 int lbWidth
= GetCount() > 0 ? 20 : 100; // some defaults
267 GetThemeMetric(kThemeMetricPopupButtonHeight
, &metric
);
271 wxMacPortStateHelper
st( UMAGetWindowPort( (WindowRef
) MacGetTopLevelWindowRef() ) ) ;
274 ::TextFont( m_font
.MacGetFontNum() ) ;
275 ::TextSize( m_font
.MacGetFontSize() ) ;
276 ::TextFace( m_font
.MacGetFontStyle() ) ;
280 ::TextFont( kFontIDMonaco
) ;
284 // Find the widest line
285 for(int i
= 0; i
< GetCount(); i
++) {
286 wxString
str(GetString(i
));
290 ::GetThemeTextDimensions( wxMacCFStringHolder( str
, m_font
.GetEncoding() ) ,
291 kThemeCurrentPortFont
,
298 wLine
= ::TextWidth( str
.c_str() , 0 , str
.Length() ) ;
300 lbWidth
= wxMax(lbWidth
, wLine
);
302 // Add room for the popup arrow
303 lbWidth
+= 2 * lbHeight
;
304 // And just a bit more
305 int cx
= ::TextWidth( "X" , 0 , 1 ) ;
309 return wxSize(lbWidth
, lbHeight
);
312 #endif // wxUSE_CHOICE