1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/mac/classic/choice.cpp
4 // Author: Stefan Csomor
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
16 #include "wx/choice.h"
22 #include "wx/mac/uma.h"
24 IMPLEMENT_DYNAMIC_CLASS(wxChoice
, wxControl
)
26 extern MenuHandle
NewUniqueMenu() ;
30 if ( HasClientObjectData() )
32 unsigned int i
, max
= GetCount();
34 for ( i
= 0; i
< max
; ++i
)
35 delete GetClientObject(i
);
38 // DeleteMenu( m_macPopUpMenuId ) ;
39 // DisposeMenu( m_macPopUpMenuHandle ) ;
42 bool wxChoice::Create(wxWindow
*parent
, wxWindowID id
,
45 const wxArrayString
& choices
,
47 const wxValidator
& validator
,
50 wxCArrayString
chs(choices
);
52 return Create(parent
, id
, pos
, size
, chs
.GetCount(), chs
.GetStrings(),
53 style
, validator
, name
);
56 bool wxChoice::Create(wxWindow
*parent
, wxWindowID id
,
59 int n
, const wxString choices
[],
61 const wxValidator
& validator
,
64 if ( !wxChoiceBase::Create(parent
, id
, pos
, size
, style
, validator
, name
) )
70 MacPreControlCreate( parent
, id
, wxEmptyString
, pos
, size
,style
, validator
, name
, &bounds
, title
) ;
71 m_macControl
= (WXWidget
) ::NewControl( MAC_WXHWND(parent
->MacGetRootWindow()) , &bounds
, title
, false , 0 , -12345 , 0 ,
72 kControlPopupButtonProc
+ kControlPopupFixedWidthVariant
, (long) this ) ;
74 m_macPopUpMenuHandle
= NewUniqueMenu() ;
75 SetControlData( (ControlHandle
) m_macControl
, kControlNoPart
, kControlPopupButtonMenuHandleTag
, sizeof( MenuHandle
) , (char*) &m_macPopUpMenuHandle
) ;
76 SetControl32BitMinimum( (ControlHandle
) m_macControl
, 0 ) ;
77 SetControl32BitMaximum( (ControlHandle
) m_macControl
, 0) ;
79 SetControl32BitValue( (ControlHandle
) m_macControl
, 1 ) ;
80 MacPostControlCreate() ;
82 for ( int i
= 0; i
< n
; i
++ )
89 // ----------------------------------------------------------------------------
90 // adding/deleting items to/from the list
91 // ----------------------------------------------------------------------------
92 int wxChoice::DoAppend(const wxString
& item
)
94 UMAAppendMenuItem(MAC_WXHMENU( m_macPopUpMenuHandle
) , item
, m_font
.GetEncoding() );
95 m_strings
.Add( item
) ;
97 int index
= m_strings
.GetCount() - 1 ;
98 DoSetItemClientData( index
, NULL
) ;
99 SetControl32BitMaximum( (ControlHandle
) m_macControl
, GetCount()) ;
103 int wxChoice::DoInsert(const wxString
& item
, unsigned int pos
)
105 wxCHECK_MSG(!(GetWindowStyle() & wxCB_SORT
), -1, wxT("can't insert into sorted list"));
106 wxCHECK_MSG(IsValidInsert(pos
), -1, wxT("invalid index"));
108 if (pos
== GetCount())
109 return DoAppend(item
);
111 UMAAppendMenuItem(MAC_WXHMENU( m_macPopUpMenuHandle
) , item
, m_font
.GetEncoding() );
112 m_strings
.Insert( item
, pos
) ;
113 m_datas
.Insert( NULL
, pos
) ;
114 DoSetItemClientData( pos
, NULL
) ;
115 SetControl32BitMaximum( (ControlHandle
) m_macControl
, pos
) ;
119 void wxChoice::Delete(unsigned int n
)
121 wxCHECK_RET( IsValid(n
), wxT("invalid item index in wxChoice::Delete") );
122 if ( HasClientObjectData() )
124 delete GetClientObject(n
);
126 ::DeleteMenuItem( MAC_WXHMENU(m_macPopUpMenuHandle
) , n
+ 1) ;
127 m_strings
.RemoveAt( n
) ;
128 m_datas
.RemoveAt( n
) ;
129 SetControl32BitMaximum( (ControlHandle
) m_macControl
, GetCount()) ;
132 void wxChoice::Clear()
135 for ( unsigned int i
= 0 ; i
< GetCount() ; i
++ )
137 ::DeleteMenuItem( MAC_WXHMENU(m_macPopUpMenuHandle
) , 1 ) ;
141 SetControl32BitMaximum( (ControlHandle
) m_macControl
, 0 ) ;
144 void wxChoice::FreeData()
146 if ( HasClientObjectData() )
148 unsigned int count
= GetCount();
149 for ( unsigned int n
= 0; n
< count
; n
++ )
151 delete GetClientObject(n
);
156 // ----------------------------------------------------------------------------
158 // ----------------------------------------------------------------------------
159 int wxChoice::GetSelection() const
161 return GetControl32BitValue( (ControlHandle
) m_macControl
) -1 ;
164 void wxChoice::SetSelection(int n
)
166 SetControl32BitValue( (ControlHandle
) m_macControl
, n
+ 1 ) ;
169 // ----------------------------------------------------------------------------
170 // string list functions
171 // ----------------------------------------------------------------------------
173 unsigned int wxChoice::GetCount() const
175 return m_strings
.GetCount() ;
178 void wxChoice::SetString(unsigned int n
, const wxString
& s
)
180 wxFAIL_MSG(wxT("wxChoice::SetString() not yet implemented"));
181 #if 0 // should do this, but no Insert() so far
187 wxString
wxChoice::GetString(unsigned int n
) const
189 wxCHECK_MSG( IsValid(n
), wxEmptyString
,
190 _T("wxChoice::GetString(): invalid index") );
192 return m_strings
[n
] ;
195 // ----------------------------------------------------------------------------
197 // ----------------------------------------------------------------------------
198 void wxChoice::DoSetItemClientData(unsigned int n
, void* clientData
)
200 wxCHECK_RET( n
>= 0 && (unsigned int)n
< m_datas
.GetCount(),
201 wxT("invalid index in wxChoice::SetClientData") );
203 m_datas
[n
] = (char*) clientData
;
206 void *wxChoice::DoGetItemClientData(unsigned int n
) const
208 wxCHECK_MSG( n
>= 0 && (unsigned int)n
< m_datas
.GetCount(), NULL
,
209 wxT("invalid index in wxChoice::GetClientData") );
210 return (void *)m_datas
[n
];
213 void wxChoice::DoSetItemClientObject(unsigned int n
, wxClientData
* clientData
)
215 DoSetItemClientData(n
, clientData
);
218 wxClientData
* wxChoice::DoGetItemClientObject(unsigned int n
) const
220 return (wxClientData
*)DoGetItemClientData(n
);
223 void wxChoice::MacHandleControlClick( WXWidget control
, wxInt16 controlpart
, bool WXUNUSED(mouseStillDown
))
225 wxCommandEvent
event(wxEVT_COMMAND_CHOICE_SELECTED
, m_windowId
);
226 int n
= GetSelection();
227 // actually n should be made sure by the os to be a valid selection, but ...
231 event
.SetString(GetStringSelection());
232 event
.SetEventObject(this);
233 if ( HasClientObjectData() )
234 event
.SetClientObject( GetClientObject(n
) );
235 else if ( HasClientUntypedData() )
236 event
.SetClientData( GetClientData(n
) );
237 ProcessCommand(event
);
241 wxSize
wxChoice::DoGetBestSize() const
243 int lbWidth
= GetCount() > 0 ? 20 : 100; // some defaults
248 GetThemeMetric(kThemeMetricPopupButtonHeight
, &metric
);
252 wxMacPortStateHelper
st( UMAGetWindowPort( (WindowRef
) MacGetRootWindow() ) ) ;
255 ::TextFont( m_font
.GetMacFontNum() ) ;
256 ::TextSize( m_font
.GetMacFontSize() ) ;
257 ::TextFace( m_font
.GetMacFontStyle() ) ;
261 ::TextFont( kFontIDMonaco
) ;
265 // Find the widest line
266 for(unsigned int i
= 0; i
< GetCount(); i
++) {
267 wxString
str(GetString(i
));
271 ::GetThemeTextDimensions( wxMacCFStringHolder( str
, m_font
.GetEncoding() ) ,
272 kThemeCurrentPortFont
,
279 wLine
= ::TextWidth( str
.c_str() , 0 , str
.length() ) ;
281 lbWidth
= wxMax(lbWidth
, wLine
);
283 // Add room for the popup arrow
284 lbWidth
+= 2 * lbHeight
;
285 // And just a bit more
286 int cx
= ::TextWidth( "X" , 0 , 1 ) ;
290 return wxSize(lbWidth
, lbHeight
);
293 #endif // wxUSE_CHOICE