1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Stefan Csomor
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
16 #include "wx/choice.h"
18 #include "wx/mac/uma.h"
20 IMPLEMENT_DYNAMIC_CLASS(wxChoice
, wxControl
)
22 extern MenuHandle
NewUniqueMenu() ;
26 if ( HasClientObjectData() )
28 size_t i
, max
= GetCount();
30 for ( i
= 0; i
< max
; ++i
)
31 delete GetClientObject(i
);
34 // DeleteMenu( m_macPopUpMenuId ) ;
35 // DisposeMenu( m_macPopUpMenuHandle ) ;
38 bool wxChoice::Create(wxWindow
*parent
, wxWindowID id
,
41 const wxArrayString
& choices
,
43 const wxValidator
& validator
,
46 wxCArrayString
chs(choices
);
48 return Create(parent
, id
, pos
, size
, chs
.GetCount(), chs
.GetStrings(),
49 style
, validator
, name
);
52 bool wxChoice::Create(wxWindow
*parent
, wxWindowID id
,
55 int n
, const wxString choices
[],
57 const wxValidator
& validator
,
60 if ( !wxChoiceBase::Create(parent
, id
, pos
, size
, style
, validator
, name
) )
66 MacPreControlCreate( parent
, id
, wxEmptyString
, pos
, size
,style
, validator
, name
, &bounds
, title
) ;
67 m_macControl
= (WXWidget
) ::NewControl( MAC_WXHWND(parent
->MacGetRootWindow()) , &bounds
, title
, false , 0 , -12345 , 0 ,
68 kControlPopupButtonProc
+ kControlPopupFixedWidthVariant
, (long) this ) ;
70 m_macPopUpMenuHandle
= NewUniqueMenu() ;
71 SetControlData( (ControlHandle
) m_macControl
, kControlNoPart
, kControlPopupButtonMenuHandleTag
, sizeof( MenuHandle
) , (char*) &m_macPopUpMenuHandle
) ;
72 SetControl32BitMinimum( (ControlHandle
) m_macControl
, 0 ) ;
73 SetControl32BitMaximum( (ControlHandle
) m_macControl
, 0) ;
75 SetControl32BitValue( (ControlHandle
) m_macControl
, 1 ) ;
76 MacPostControlCreate() ;
78 for ( int i
= 0; i
< n
; i
++ )
85 // ----------------------------------------------------------------------------
86 // adding/deleting items to/from the list
87 // ----------------------------------------------------------------------------
88 int wxChoice::DoAppend(const wxString
& item
)
90 UMAAppendMenuItem(MAC_WXHMENU( m_macPopUpMenuHandle
) , item
, m_font
.GetEncoding() );
91 m_strings
.Add( item
) ;
93 int index
= m_strings
.GetCount() - 1 ;
94 DoSetItemClientData( index
, NULL
) ;
95 SetControl32BitMaximum( (ControlHandle
) m_macControl
, GetCount()) ;
99 int wxChoice::DoInsert(const wxString
& item
, int pos
)
101 wxCHECK_MSG(!(GetWindowStyle() & wxCB_SORT
), -1, wxT("can't insert into sorted list"));
102 wxCHECK_MSG((pos
>=0) && (pos
<=GetCount()), -1, wxT("invalid index"));
104 if (pos
== GetCount())
105 return DoAppend(item
);
107 UMAAppendMenuItem(MAC_WXHMENU( m_macPopUpMenuHandle
) , item
, m_font
.GetEncoding() );
108 m_strings
.Insert( item
, pos
) ;
109 m_datas
.Insert( NULL
, pos
) ;
110 DoSetItemClientData( pos
, NULL
) ;
111 SetControl32BitMaximum( (ControlHandle
) m_macControl
, pos
) ;
115 void wxChoice::Delete(int n
)
117 wxCHECK_RET( n
< GetCount(), wxT("invalid item index in wxChoice::Delete") );
118 if ( HasClientObjectData() )
120 delete GetClientObject(n
);
122 ::DeleteMenuItem( MAC_WXHMENU(m_macPopUpMenuHandle
) , n
+ 1) ;
123 m_strings
.RemoveAt( n
) ;
124 m_datas
.RemoveAt( n
) ;
125 SetControl32BitMaximum( (ControlHandle
) m_macControl
, GetCount()) ;
128 void wxChoice::Clear()
131 for ( int i
= 0 ; i
< GetCount() ; i
++ )
133 ::DeleteMenuItem( MAC_WXHMENU(m_macPopUpMenuHandle
) , 1 ) ;
137 SetControl32BitMaximum( (ControlHandle
) m_macControl
, 0 ) ;
140 void wxChoice::FreeData()
142 if ( HasClientObjectData() )
144 size_t count
= GetCount();
145 for ( size_t n
= 0; n
< count
; n
++ )
147 delete GetClientObject(n
);
152 // ----------------------------------------------------------------------------
154 // ----------------------------------------------------------------------------
155 int wxChoice::GetSelection() const
157 return GetControl32BitValue( (ControlHandle
) m_macControl
) -1 ;
160 void wxChoice::SetSelection(int n
)
162 SetControl32BitValue( (ControlHandle
) m_macControl
, n
+ 1 ) ;
165 // ----------------------------------------------------------------------------
166 // string list functions
167 // ----------------------------------------------------------------------------
169 int wxChoice::GetCount() const
171 return m_strings
.GetCount() ;
174 int wxChoice::FindString(const wxString
& s
) const
176 for( int i
= 0 ; i
< GetCount() ; i
++ )
178 if ( GetString( i
).IsSameAs(s
, false) )
184 void wxChoice::SetString(int n
, const wxString
& s
)
186 wxFAIL_MSG(wxT("wxChoice::SetString() not yet implemented"));
187 #if 0 // should do this, but no Insert() so far
193 wxString
wxChoice::GetString(int n
) const
195 wxCHECK_MSG( n
>= 0 && (size_t)n
< m_strings
.GetCount(), _T(""),
196 _T("wxChoice::GetString(): invalid index") );
198 return m_strings
[n
] ;
201 // ----------------------------------------------------------------------------
203 // ----------------------------------------------------------------------------
204 void wxChoice::DoSetItemClientData( int n
, void* clientData
)
206 wxCHECK_RET( n
>= 0 && (size_t)n
< m_datas
.GetCount(),
207 wxT("invalid index in wxChoice::SetClientData") );
209 m_datas
[n
] = (char*) clientData
;
212 void *wxChoice::DoGetItemClientData(int n
) const
214 wxCHECK_MSG( n
>= 0 && (size_t)n
< m_datas
.GetCount(), NULL
,
215 wxT("invalid index in wxChoice::GetClientData") );
216 return (void *)m_datas
[n
];
219 void wxChoice::DoSetItemClientObject( int n
, wxClientData
* clientData
)
221 DoSetItemClientData(n
, clientData
);
224 wxClientData
* wxChoice::DoGetItemClientObject( int n
) const
226 return (wxClientData
*)DoGetItemClientData(n
);
229 void wxChoice::MacHandleControlClick( WXWidget control
, wxInt16 controlpart
, bool WXUNUSED(mouseStillDown
))
231 wxCommandEvent
event(wxEVT_COMMAND_CHOICE_SELECTED
, m_windowId
);
232 int n
= GetSelection();
233 // actually n should be made sure by the os to be a valid selection, but ...
237 event
.SetString(GetStringSelection());
238 event
.SetEventObject(this);
239 if ( HasClientObjectData() )
240 event
.SetClientObject( GetClientObject(n
) );
241 else if ( HasClientUntypedData() )
242 event
.SetClientData( GetClientData(n
) );
243 ProcessCommand(event
);
247 wxSize
wxChoice::DoGetBestSize() const
249 int lbWidth
= GetCount() > 0 ? 20 : 100; // some defaults
254 GetThemeMetric(kThemeMetricPopupButtonHeight
, &metric
);
258 wxMacPortStateHelper
st( UMAGetWindowPort( (WindowRef
) MacGetRootWindow() ) ) ;
261 ::TextFont( m_font
.GetMacFontNum() ) ;
262 ::TextSize( m_font
.GetMacFontSize() ) ;
263 ::TextFace( m_font
.GetMacFontStyle() ) ;
267 ::TextFont( kFontIDMonaco
) ;
271 // Find the widest line
272 for(int i
= 0; i
< GetCount(); i
++) {
273 wxString
str(GetString(i
));
277 ::GetThemeTextDimensions( wxMacCFStringHolder( str
, m_font
.GetEncoding() ) ,
278 kThemeCurrentPortFont
,
285 wLine
= ::TextWidth( str
.c_str() , 0 , str
.Length() ) ;
287 lbWidth
= wxMax(lbWidth
, wLine
);
289 // Add room for the popup arrow
290 lbWidth
+= 2 * lbHeight
;
291 // And just a bit more
292 int cx
= ::TextWidth( "X" , 0 , 1 ) ;
296 return wxSize(lbWidth
, lbHeight
);
299 #endif // wxUSE_CHOICE