1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Stefan Csomor
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "choice.h"
17 #include "wx/choice.h"
19 #include "wx/mac/uma.h"
21 #if !USE_SHARED_LIBRARY
22 IMPLEMENT_DYNAMIC_CLASS(wxChoice
, wxControl
)
25 extern MenuHandle
NewUniqueMenu() ;
29 if ( HasClientObjectData() )
31 size_t i
, max
= GetCount();
33 for ( i
= 0; i
< max
; ++i
)
34 delete GetClientObject(i
);
37 // DeleteMenu( m_macPopUpMenuId ) ;
38 // DisposeMenu( m_macPopUpMenuHandle ) ;
41 bool wxChoice::Create(wxWindow
*parent
, wxWindowID id
,
44 int n
, const wxString choices
[],
46 const wxValidator
& validator
,
49 if ( !wxChoiceBase::Create(parent
, id
, pos
, size
, style
, validator
, name
) )
55 MacPreControlCreate( parent
, id
, wxEmptyString
, pos
, size
,style
, validator
, name
, &bounds
, title
) ;
56 m_macControl
= ::NewControl( MAC_WXHWND(parent
->MacGetRootWindow()) , &bounds
, title
, false , 0 , -12345 , 0 ,
57 kControlPopupButtonProc
+ kControlPopupFixedWidthVariant
, (long) this ) ;
59 m_macPopUpMenuHandle
= NewUniqueMenu() ;
60 SetControlData( (ControlHandle
) m_macControl
, kControlNoPart
, kControlPopupButtonMenuHandleTag
, sizeof( MenuHandle
) , (char*) &m_macPopUpMenuHandle
) ;
61 SetControl32BitMinimum( (ControlHandle
) m_macControl
, 0 ) ;
62 SetControl32BitMaximum( (ControlHandle
) m_macControl
, 0) ;
64 SetControl32BitValue( (ControlHandle
) m_macControl
, 1 ) ;
65 MacPostControlCreate() ;
67 for ( int i
= 0; i
< n
; i
++ )
74 // ----------------------------------------------------------------------------
75 // adding/deleting items to/from the list
76 // ----------------------------------------------------------------------------
77 int wxChoice::DoAppend(const wxString
& item
)
79 UMAAppendMenuItem(MAC_WXHMENU( m_macPopUpMenuHandle
) , item
);
80 m_strings
.Add( item
) ;
82 int index
= m_strings
.GetCount() - 1 ;
83 DoSetItemClientData( index
, NULL
) ;
84 SetControl32BitMaximum( (ControlHandle
) m_macControl
, GetCount()) ;
88 int wxChoice::DoInsert(const wxString
& item
, int pos
)
90 wxCHECK_MSG(!(GetWindowStyle() & wxCB_SORT
), -1, wxT("can't insert into sorted list"));
91 wxCHECK_MSG((pos
>=0) && (pos
<=GetCount()), -1, wxT("invalid index"));
93 if (pos
== GetCount())
94 return DoAppend(item
);
96 UMAAppendMenuItem(MAC_WXHMENU( m_macPopUpMenuHandle
) , item
);
97 m_strings
.Insert( item
, pos
) ;
98 m_datas
.Insert( NULL
, pos
) ;
99 DoSetItemClientData( pos
, NULL
) ;
100 SetControl32BitMaximum( (ControlHandle
) m_macControl
, pos
) ;
104 void wxChoice::Delete(int n
)
106 wxCHECK_RET( n
< GetCount(), wxT("invalid item index in wxChoice::Delete") );
107 if ( HasClientObjectData() )
109 delete GetClientObject(n
);
111 ::DeleteMenuItem( MAC_WXHMENU(m_macPopUpMenuHandle
) , n
+ 1) ;
112 m_strings
.RemoveAt( n
) ;
113 m_datas
.RemoveAt( n
) ;
114 SetControl32BitMaximum( (ControlHandle
) m_macControl
, GetCount()) ;
117 void wxChoice::Clear()
120 for ( int i
= 0 ; i
< GetCount() ; i
++ )
122 ::DeleteMenuItem( MAC_WXHMENU(m_macPopUpMenuHandle
) , 1 ) ;
126 SetControl32BitMaximum( (ControlHandle
) m_macControl
, 0 ) ;
129 void wxChoice::FreeData()
131 if ( HasClientObjectData() )
133 size_t count
= GetCount();
134 for ( size_t n
= 0; n
< count
; n
++ )
136 delete GetClientObject(n
);
141 // ----------------------------------------------------------------------------
143 // ----------------------------------------------------------------------------
144 int wxChoice::GetSelection() const
146 return GetControl32BitValue( (ControlHandle
) m_macControl
) -1 ;
149 void wxChoice::SetSelection(int n
)
151 SetControl32BitValue( (ControlHandle
) m_macControl
, n
+ 1 ) ;
154 // ----------------------------------------------------------------------------
155 // string list functions
156 // ----------------------------------------------------------------------------
158 int wxChoice::GetCount() const
160 return m_strings
.GetCount() ;
163 int wxChoice::FindString(const wxString
& s
) const
165 for( int i
= 0 ; i
< GetCount() ; i
++ )
167 if ( GetString( i
).IsSameAs(s
, FALSE
) )
173 void wxChoice::SetString(int n
, const wxString
& s
)
175 wxFAIL_MSG(wxT("wxChoice::SetString() not yet implemented"));
176 #if 0 // should do this, but no Insert() so far
182 wxString
wxChoice::GetString(int n
) const
184 wxCHECK_MSG( n
>= 0 && (size_t)n
< m_strings
.GetCount(), _T(""),
185 _T("wxChoice::GetString(): invalid index") );
187 return m_strings
[n
] ;
190 // ----------------------------------------------------------------------------
192 // ----------------------------------------------------------------------------
193 void wxChoice::DoSetItemClientData( int n
, void* clientData
)
195 wxCHECK_RET( n
>= 0 && (size_t)n
< m_datas
.GetCount(),
196 wxT("invalid index in wxChoice::SetClientData") );
198 m_datas
[n
] = (char*) clientData
;
201 void *wxChoice::DoGetItemClientData(int n
) const
203 wxCHECK_MSG( n
>= 0 && (size_t)n
< m_datas
.GetCount(), NULL
,
204 wxT("invalid index in wxChoice::GetClientData") );
205 return (void *)m_datas
[n
];
208 void wxChoice::DoSetItemClientObject( int n
, wxClientData
* clientData
)
210 DoSetItemClientData(n
, clientData
);
213 wxClientData
* wxChoice::DoGetItemClientObject( int n
) const
215 return (wxClientData
*)DoGetItemClientData(n
);
218 void wxChoice::MacHandleControlClick( WXWidget control
, wxInt16 controlpart
, bool WXUNUSED(mouseStillDown
))
220 wxCommandEvent
event(wxEVT_COMMAND_CHOICE_SELECTED
, m_windowId
);
221 int n
= GetSelection();
222 // actually n should be made sure by the os to be a valid selection, but ...
226 event
.SetString(GetStringSelection());
227 event
.SetEventObject(this);
228 if ( HasClientObjectData() )
229 event
.SetClientObject( GetClientObject(n
) );
230 else if ( HasClientUntypedData() )
231 event
.SetClientData( GetClientData(n
) );
232 ProcessCommand(event
);
236 wxSize
wxChoice::DoGetBestSize() const
238 int lbWidth
= GetCount() > 0 ? 20 : 100; // some defaults
243 GetThemeMetric(kThemeMetricPopupButtonHeight
, &metric
);
247 wxMacPortStateHelper
st( UMAGetWindowPort( (WindowRef
) MacGetRootWindow() ) ) ;
250 ::TextFont( m_font
.GetMacFontNum() ) ;
251 ::TextSize( m_font
.GetMacFontSize() ) ;
252 ::TextFace( m_font
.GetMacFontStyle() ) ;
256 ::TextFont( kFontIDMonaco
) ;
260 // Find the widest line
261 for(int i
= 0; i
< GetCount(); i
++) {
262 wxString
str(GetString(i
));
266 ::GetThemeTextDimensions( wxMacCFStringHolder( str
) ,
267 kThemeCurrentPortFont
,
274 wLine
= ::TextWidth( str
.c_str() , 0 , str
.Length() ) ;
276 lbWidth
= wxMax(lbWidth
, wLine
);
278 // Add room for the popup arrow
279 lbWidth
+= 2 * lbHeight
;
280 // And just a bit more
281 int cx
= ::TextWidth( "X" , 0 , 1 ) ;
285 return wxSize(lbWidth
, lbHeight
);