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() ;
66 for ( int i
= 0; i
< n
; i
++ )
73 // ----------------------------------------------------------------------------
74 // adding/deleting items to/from the list
75 // ----------------------------------------------------------------------------
76 int wxChoice::DoAppend(const wxString
& item
)
78 UMAAppendMenuItem(MAC_WXHMENU( m_macPopUpMenuHandle
) , item
);
79 m_strings
.Add( item
) ;
81 int index
= m_strings
.GetCount() - 1 ;
82 DoSetItemClientData( index
, NULL
) ;
83 SetControl32BitMaximum( (ControlHandle
) m_macControl
, GetCount()) ;
87 int wxChoice::DoInsert(const wxString
& item
, int pos
)
89 wxCHECK_MSG(!(GetWindowStyle() & wxCB_SORT
), -1, wxT("can't insert into sorted list"));
90 wxCHECK_MSG((pos
>=0) && (pos
<=GetCount()), -1, wxT("invalid index"));
92 if (pos
== GetCount())
93 return DoAppend(item
);
95 UMAAppendMenuItem(MAC_WXHMENU( m_macPopUpMenuHandle
) , item
);
96 m_strings
.Insert( item
, pos
) ;
97 m_datas
.Insert( NULL
, pos
) ;
98 DoSetItemClientData( pos
, NULL
) ;
99 SetControl32BitMaximum( (ControlHandle
) m_macControl
, pos
) ;
103 void wxChoice::Delete(int n
)
105 wxCHECK_RET( n
< GetCount(), wxT("invalid item index in wxChoice::Delete") );
106 if ( HasClientObjectData() )
108 delete GetClientObject(n
);
110 ::DeleteMenuItem( MAC_WXHMENU(m_macPopUpMenuHandle
) , n
+ 1) ;
111 m_strings
.RemoveAt( n
) ;
112 m_datas
.RemoveAt( n
) ;
113 SetControl32BitMaximum( (ControlHandle
) m_macControl
, GetCount()) ;
116 void wxChoice::Clear()
119 for ( int i
= 0 ; i
< GetCount() ; i
++ )
121 ::DeleteMenuItem( MAC_WXHMENU(m_macPopUpMenuHandle
) , 1 ) ;
125 SetControl32BitMaximum( (ControlHandle
) m_macControl
, 0 ) ;
128 void wxChoice::FreeData()
130 if ( HasClientObjectData() )
132 size_t count
= GetCount();
133 for ( size_t n
= 0; n
< count
; n
++ )
135 delete GetClientObject(n
);
140 // ----------------------------------------------------------------------------
142 // ----------------------------------------------------------------------------
143 int wxChoice::GetSelection() const
145 return GetControl32BitValue( (ControlHandle
) m_macControl
) -1 ;
148 void wxChoice::SetSelection(int n
)
150 SetControl32BitValue( (ControlHandle
) m_macControl
, n
+ 1 ) ;
153 // ----------------------------------------------------------------------------
154 // string list functions
155 // ----------------------------------------------------------------------------
157 int wxChoice::GetCount() const
159 return m_strings
.GetCount() ;
162 int wxChoice::FindString(const wxString
& s
) const
164 for( int i
= 0 ; i
< GetCount() ; i
++ )
166 if ( GetString( i
).IsSameAs(s
, FALSE
) )
172 void wxChoice::SetString(int n
, const wxString
& s
)
174 wxFAIL_MSG(wxT("wxChoice::SetString() not yet implemented"));
175 #if 0 // should do this, but no Insert() so far
181 wxString
wxChoice::GetString(int n
) const
183 wxCHECK_MSG( n
>= 0 && (size_t)n
< m_strings
.GetCount(), _T(""),
184 _T("wxChoice::GetString(): invalid index") );
186 return m_strings
[n
] ;
189 // ----------------------------------------------------------------------------
191 // ----------------------------------------------------------------------------
192 void wxChoice::DoSetItemClientData( int n
, void* clientData
)
194 wxCHECK_RET( n
>= 0 && (size_t)n
< m_datas
.GetCount(),
195 wxT("invalid index in wxChoice::SetClientData") );
197 m_datas
[n
] = (char*) clientData
;
200 void *wxChoice::DoGetItemClientData(int n
) const
202 wxCHECK_MSG( n
>= 0 && (size_t)n
< m_datas
.GetCount(), NULL
,
203 wxT("invalid index in wxChoice::GetClientData") );
204 return (void *)m_datas
[n
];
207 void wxChoice::DoSetItemClientObject( int n
, wxClientData
* clientData
)
209 DoSetItemClientData(n
, clientData
);
212 wxClientData
* wxChoice::DoGetItemClientObject( int n
) const
214 return (wxClientData
*)DoGetItemClientData(n
);
217 void wxChoice::MacHandleControlClick( WXWidget control
, wxInt16 controlpart
, bool WXUNUSED(mouseStillDown
))
219 wxCommandEvent
event(wxEVT_COMMAND_CHOICE_SELECTED
, m_windowId
);
220 int n
= GetSelection();
221 // actually n should be made sure by the os to be a valid selection, but ...
225 event
.SetString(GetStringSelection());
226 event
.SetEventObject(this);
227 if ( HasClientObjectData() )
228 event
.SetClientObject( GetClientObject(n
) );
229 else if ( HasClientUntypedData() )
230 event
.SetClientData( GetClientData(n
) );
231 ProcessCommand(event
);
235 wxSize
wxChoice::DoGetBestSize() const
237 int lbWidth
= GetCount() > 0 ? 20 : 100; // some defaults
242 GetThemeMetric(kThemeMetricPopupButtonHeight
, &metric
);
246 wxMacPortStateHelper
st( UMAGetWindowPort( (WindowRef
) MacGetRootWindow() ) ) ;
247 wxFontRefData
* font
= (wxFontRefData
*) m_font
.GetRefData() ;
250 ::TextFont( font
->m_macFontNum
) ;
251 ::TextSize( short(font
->m_macFontSize
) ) ;
252 ::TextFace( font
->m_macFontStyle
) ;
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 wxCharBuffer text
= wxMacStringToCString( str
) ;
275 wLine
= ::TextWidth( text
, 0 , strlen(text
) ) ;
277 lbWidth
= wxMax(lbWidth
, wLine
);
279 // Add room for the popup arrow
280 lbWidth
+= 2 * lbHeight
;
281 // And just a bit more
282 int cx
= ::TextWidth( "X" , 0 , 1 ) ;
286 return wxSize(lbWidth
, lbHeight
);