1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/mac/classic/choice.cpp
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 void wxChoice::SetString(int n
, const wxString
& s
)
176 wxFAIL_MSG(wxT("wxChoice::SetString() not yet implemented"));
177 #if 0 // should do this, but no Insert() so far
183 wxString
wxChoice::GetString(int n
) const
185 wxCHECK_MSG( n
>= 0 && (size_t)n
< m_strings
.GetCount(), wxEmptyString
,
186 _T("wxChoice::GetString(): invalid index") );
188 return m_strings
[n
] ;
191 // ----------------------------------------------------------------------------
193 // ----------------------------------------------------------------------------
194 void wxChoice::DoSetItemClientData( int n
, void* clientData
)
196 wxCHECK_RET( n
>= 0 && (size_t)n
< m_datas
.GetCount(),
197 wxT("invalid index in wxChoice::SetClientData") );
199 m_datas
[n
] = (char*) clientData
;
202 void *wxChoice::DoGetItemClientData(int n
) const
204 wxCHECK_MSG( n
>= 0 && (size_t)n
< m_datas
.GetCount(), NULL
,
205 wxT("invalid index in wxChoice::GetClientData") );
206 return (void *)m_datas
[n
];
209 void wxChoice::DoSetItemClientObject( int n
, wxClientData
* clientData
)
211 DoSetItemClientData(n
, clientData
);
214 wxClientData
* wxChoice::DoGetItemClientObject( int n
) const
216 return (wxClientData
*)DoGetItemClientData(n
);
219 void wxChoice::MacHandleControlClick( WXWidget control
, wxInt16 controlpart
, bool WXUNUSED(mouseStillDown
))
221 wxCommandEvent
event(wxEVT_COMMAND_CHOICE_SELECTED
, m_windowId
);
222 int n
= GetSelection();
223 // actually n should be made sure by the os to be a valid selection, but ...
227 event
.SetString(GetStringSelection());
228 event
.SetEventObject(this);
229 if ( HasClientObjectData() )
230 event
.SetClientObject( GetClientObject(n
) );
231 else if ( HasClientUntypedData() )
232 event
.SetClientData( GetClientData(n
) );
233 ProcessCommand(event
);
237 wxSize
wxChoice::DoGetBestSize() const
239 int lbWidth
= GetCount() > 0 ? 20 : 100; // some defaults
244 GetThemeMetric(kThemeMetricPopupButtonHeight
, &metric
);
248 wxMacPortStateHelper
st( UMAGetWindowPort( (WindowRef
) MacGetRootWindow() ) ) ;
251 ::TextFont( m_font
.GetMacFontNum() ) ;
252 ::TextSize( m_font
.GetMacFontSize() ) ;
253 ::TextFace( m_font
.GetMacFontStyle() ) ;
257 ::TextFont( kFontIDMonaco
) ;
261 // Find the widest line
262 for(int i
= 0; i
< GetCount(); i
++) {
263 wxString
str(GetString(i
));
267 ::GetThemeTextDimensions( wxMacCFStringHolder( str
, m_font
.GetEncoding() ) ,
268 kThemeCurrentPortFont
,
275 wLine
= ::TextWidth( str
.c_str() , 0 , str
.Length() ) ;
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
);
289 #endif // wxUSE_CHOICE