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 if ( style
& wxCB_SORT
)
85 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
)
101 size_t index
= m_strings
.Add( item
) ;
102 m_datas
.Insert( NULL
, index
) ;
103 UMAInsertMenuItem(MAC_WXHMENU( m_macPopUpMenuHandle
) , item
, m_font
.GetEncoding() , index
);
104 DoSetItemClientData( index
, NULL
) ;
105 m_peer
->SetMaximum( GetCount() ) ;
109 int wxChoice::DoInsert(const wxString
& item
, int pos
)
111 wxCHECK_MSG(!(GetWindowStyle() & wxCB_SORT
), -1, wxT("can't insert into sorted list"));
112 wxCHECK_MSG((pos
>=0) && (pos
<=GetCount()), -1, wxT("invalid index"));
114 if (pos
== GetCount())
115 return DoAppend(item
);
117 UMAInsertMenuItem(MAC_WXHMENU( m_macPopUpMenuHandle
) , item
, m_font
.GetEncoding() , pos
);
118 m_strings
.Insert( item
, pos
) ;
119 m_datas
.Insert( NULL
, pos
) ;
120 DoSetItemClientData( pos
, NULL
) ;
121 m_peer
->SetMaximum( GetCount() ) ;
125 void wxChoice::Delete(int n
)
127 wxCHECK_RET( n
< GetCount(), wxT("invalid item index in wxChoice::Delete") );
128 if ( HasClientObjectData() )
130 delete GetClientObject(n
);
132 ::DeleteMenuItem( MAC_WXHMENU(m_macPopUpMenuHandle
) , n
+ 1) ;
133 m_strings
.RemoveAt( n
) ;
134 m_datas
.RemoveAt( n
) ;
135 m_peer
->SetMaximum( GetCount() ) ;
138 void wxChoice::Clear()
141 for ( int i
= 0 ; i
< GetCount() ; i
++ )
143 ::DeleteMenuItem( MAC_WXHMENU(m_macPopUpMenuHandle
) , 1 ) ;
147 m_peer
->SetMaximum( 0 ) ;
150 void wxChoice::FreeData()
152 if ( HasClientObjectData() )
154 size_t count
= GetCount();
155 for ( size_t n
= 0; n
< count
; n
++ )
157 delete GetClientObject(n
);
162 // ----------------------------------------------------------------------------
164 // ----------------------------------------------------------------------------
165 int wxChoice::GetSelection() const
167 return m_peer
->GetValue() -1 ;
170 void wxChoice::SetSelection(int n
)
172 m_peer
->SetValue( n
+ 1 ) ;
175 // ----------------------------------------------------------------------------
176 // string list functions
177 // ----------------------------------------------------------------------------
179 int wxChoice::GetCount() const
181 return m_strings
.GetCount() ;
184 int wxChoice::FindString(const wxString
& s
) const
186 return m_strings
.Index( s
, true , false) ;
189 void wxChoice::SetString(int n
, const wxString
& s
)
192 // apple menu pos is 1-based
193 UMASetMenuItemText( MAC_WXHMENU(m_macPopUpMenuHandle
) , n
+ 1 , s
, wxFont::GetDefaultEncoding() ) ;
196 wxString
wxChoice::GetString(int n
) const
198 wxCHECK_MSG( n
>= 0 && (size_t)n
< m_strings
.GetCount(), _T(""),
199 _T("wxChoice::GetString(): invalid index") );
201 return m_strings
[n
] ;
204 // ----------------------------------------------------------------------------
206 // ----------------------------------------------------------------------------
207 void wxChoice::DoSetItemClientData( int n
, void* clientData
)
209 wxCHECK_RET( n
>= 0 && (size_t)n
< m_datas
.GetCount(),
210 wxT("invalid index in wxChoice::SetClientData") );
212 m_datas
[n
] = (char*) clientData
;
215 void *wxChoice::DoGetItemClientData(int n
) const
217 wxCHECK_MSG( n
>= 0 && (size_t)n
< m_datas
.GetCount(), NULL
,
218 wxT("invalid index in wxChoice::GetClientData") );
219 return (void *)m_datas
[n
];
222 void wxChoice::DoSetItemClientObject( int n
, wxClientData
* clientData
)
224 DoSetItemClientData(n
, clientData
);
227 wxClientData
* wxChoice::DoGetItemClientObject( int n
) const
229 return (wxClientData
*)DoGetItemClientData(n
);
232 wxInt32
wxChoice::MacControlHit(WXEVENTHANDLERREF
WXUNUSED(handler
) , WXEVENTREF
WXUNUSED(event
) )
234 wxCommandEvent
event(wxEVT_COMMAND_CHOICE_SELECTED
, m_windowId
);
235 int n
= GetSelection();
236 // actually n should be made sure by the os to be a valid selection, but ...
240 event
.SetString(GetStringSelection());
241 event
.SetEventObject(this);
242 if ( HasClientObjectData() )
243 event
.SetClientObject( GetClientObject(n
) );
244 else if ( HasClientUntypedData() )
245 event
.SetClientData( GetClientData(n
) );
246 ProcessCommand(event
);
251 wxSize
wxChoice::DoGetBestSize() const
253 int lbWidth
= GetCount() > 0 ? 20 : 100; // some defaults
258 GetThemeMetric(kThemeMetricPopupButtonHeight
, &metric
);
262 wxMacPortStateHelper
st( UMAGetWindowPort( (WindowRef
) MacGetTopLevelWindowRef() ) ) ;
265 ::TextFont( m_font
.MacGetFontNum() ) ;
266 ::TextSize( m_font
.MacGetFontSize() ) ;
267 ::TextFace( m_font
.MacGetFontStyle() ) ;
271 ::TextFont( kFontIDMonaco
) ;
275 // Find the widest line
276 for(int i
= 0; i
< GetCount(); i
++) {
277 wxString
str(GetString(i
));
281 ::GetThemeTextDimensions( wxMacCFStringHolder( str
, m_font
.GetEncoding() ) ,
282 kThemeCurrentPortFont
,
289 wLine
= ::TextWidth( str
.c_str() , 0 , str
.Length() ) ;
291 lbWidth
= wxMax(lbWidth
, wLine
);
293 // Add room for the popup arrow
294 lbWidth
+= 2 * lbHeight
;
295 // And just a bit more
296 int cx
= ::TextWidth( "X" , 0 , 1 ) ;
300 return wxSize(lbWidth
, lbHeight
);
303 #endif // wxUSE_CHOICE