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 const wxArrayString
& choices
,
46 const wxValidator
& validator
,
49 wxCArrayString
chs(choices
);
51 return Create(parent
, id
, pos
, size
, chs
.GetCount(), chs
.GetStrings(),
52 style
, validator
, name
);
55 bool wxChoice::Create(wxWindow
*parent
, wxWindowID id
,
58 int n
, const wxString choices
[],
60 const wxValidator
& validator
,
63 m_macIsUserPane
= FALSE
;
65 if ( !wxChoiceBase::Create(parent
, id
, pos
, size
, style
, validator
, name
) )
68 Rect bounds
= wxMacGetBoundsForControl( this , pos
, size
) ;
69 m_macControl
= (WXWidget
) ::NewControl( MAC_WXHWND(parent
->MacGetTopLevelWindowRef()) , &bounds
, "\p" , true , 0 , -12345 , 0 ,
70 kControlPopupButtonProc
+ kControlPopupFixedWidthVariant
, (long) this ) ;
72 m_macPopUpMenuHandle
= NewUniqueMenu() ;
73 SetControlData( (ControlRef
) m_macControl
, kControlNoPart
, kControlPopupButtonMenuHandleTag
, sizeof( MenuHandle
) , (char*) &m_macPopUpMenuHandle
) ;
74 SetControl32BitMinimum( (ControlRef
) m_macControl
, 0 ) ;
75 SetControl32BitMaximum( (ControlRef
) m_macControl
, 0) ;
77 SetControl32BitValue( (ControlRef
) m_macControl
, 1 ) ;
78 MacPostControlCreate(pos
,size
) ;
80 for ( int i
= 0; i
< n
; i
++ )
87 // ----------------------------------------------------------------------------
88 // adding/deleting items to/from the list
89 // ----------------------------------------------------------------------------
90 int wxChoice::DoAppend(const wxString
& item
)
92 UMAAppendMenuItem(MAC_WXHMENU( m_macPopUpMenuHandle
) , item
, m_font
.GetEncoding() );
93 m_strings
.Add( item
) ;
95 int index
= m_strings
.GetCount() - 1 ;
96 DoSetItemClientData( index
, NULL
) ;
97 SetControl32BitMaximum( (ControlRef
) m_macControl
, GetCount()) ;
101 int wxChoice::DoInsert(const wxString
& item
, int pos
)
103 wxCHECK_MSG(!(GetWindowStyle() & wxCB_SORT
), -1, wxT("can't insert into sorted list"));
104 wxCHECK_MSG((pos
>=0) && (pos
<=GetCount()), -1, wxT("invalid index"));
106 if (pos
== GetCount())
107 return DoAppend(item
);
109 UMAAppendMenuItem(MAC_WXHMENU( m_macPopUpMenuHandle
) , item
, m_font
.GetEncoding() );
110 m_strings
.Insert( item
, pos
) ;
111 m_datas
.Insert( NULL
, pos
) ;
112 DoSetItemClientData( pos
, NULL
) ;
113 SetControl32BitMaximum( (ControlRef
) m_macControl
, pos
) ;
117 void wxChoice::Delete(int n
)
119 wxCHECK_RET( n
< GetCount(), wxT("invalid item index in wxChoice::Delete") );
120 if ( HasClientObjectData() )
122 delete GetClientObject(n
);
124 ::DeleteMenuItem( MAC_WXHMENU(m_macPopUpMenuHandle
) , n
+ 1) ;
125 m_strings
.RemoveAt( n
) ;
126 m_datas
.RemoveAt( n
) ;
127 SetControl32BitMaximum( (ControlRef
) m_macControl
, GetCount()) ;
130 void wxChoice::Clear()
133 for ( int i
= 0 ; i
< GetCount() ; i
++ )
135 ::DeleteMenuItem( MAC_WXHMENU(m_macPopUpMenuHandle
) , 1 ) ;
139 SetControl32BitMaximum( (ControlRef
) m_macControl
, 0 ) ;
142 void wxChoice::FreeData()
144 if ( HasClientObjectData() )
146 size_t count
= GetCount();
147 for ( size_t n
= 0; n
< count
; n
++ )
149 delete GetClientObject(n
);
154 // ----------------------------------------------------------------------------
156 // ----------------------------------------------------------------------------
157 int wxChoice::GetSelection() const
159 return GetControl32BitValue( (ControlRef
) m_macControl
) -1 ;
162 void wxChoice::SetSelection(int n
)
164 SetControl32BitValue( (ControlRef
) m_macControl
, n
+ 1 ) ;
167 // ----------------------------------------------------------------------------
168 // string list functions
169 // ----------------------------------------------------------------------------
171 int wxChoice::GetCount() const
173 return m_strings
.GetCount() ;
176 int wxChoice::FindString(const wxString
& s
) const
178 for( int i
= 0 ; i
< GetCount() ; i
++ )
180 if ( GetString( i
).IsSameAs(s
, FALSE
) )
186 void wxChoice::SetString(int n
, const wxString
& s
)
188 wxFAIL_MSG(wxT("wxChoice::SetString() not yet implemented"));
189 #if 0 // should do this, but no Insert() so far
195 wxString
wxChoice::GetString(int n
) const
197 wxCHECK_MSG( n
>= 0 && (size_t)n
< m_strings
.GetCount(), _T(""),
198 _T("wxChoice::GetString(): invalid index") );
200 return m_strings
[n
] ;
203 // ----------------------------------------------------------------------------
205 // ----------------------------------------------------------------------------
206 void wxChoice::DoSetItemClientData( int n
, void* clientData
)
208 wxCHECK_RET( n
>= 0 && (size_t)n
< m_datas
.GetCount(),
209 wxT("invalid index in wxChoice::SetClientData") );
211 m_datas
[n
] = (char*) clientData
;
214 void *wxChoice::DoGetItemClientData(int n
) const
216 wxCHECK_MSG( n
>= 0 && (size_t)n
< m_datas
.GetCount(), NULL
,
217 wxT("invalid index in wxChoice::GetClientData") );
218 return (void *)m_datas
[n
];
221 void wxChoice::DoSetItemClientObject( int n
, wxClientData
* clientData
)
223 DoSetItemClientData(n
, clientData
);
226 wxClientData
* wxChoice::DoGetItemClientObject( int n
) const
228 return (wxClientData
*)DoGetItemClientData(n
);
231 void wxChoice::MacHandleControlClick( WXWidget control
, wxInt16 controlpart
, bool WXUNUSED(mouseStillDown
))
233 wxCommandEvent
event(wxEVT_COMMAND_CHOICE_SELECTED
, m_windowId
);
234 int n
= GetSelection();
235 // actually n should be made sure by the os to be a valid selection, but ...
239 event
.SetString(GetStringSelection());
240 event
.SetEventObject(this);
241 if ( HasClientObjectData() )
242 event
.SetClientObject( GetClientObject(n
) );
243 else if ( HasClientUntypedData() )
244 event
.SetClientData( GetClientData(n
) );
245 ProcessCommand(event
);
249 wxSize
wxChoice::DoGetBestSize() const
251 int lbWidth
= GetCount() > 0 ? 20 : 100; // some defaults
256 GetThemeMetric(kThemeMetricPopupButtonHeight
, &metric
);
260 wxMacPortStateHelper
st( UMAGetWindowPort( (WindowRef
) MacGetTopLevelWindowRef() ) ) ;
263 ::TextFont( m_font
.MacGetFontNum() ) ;
264 ::TextSize( m_font
.MacGetFontSize() ) ;
265 ::TextFace( m_font
.MacGetFontStyle() ) ;
269 ::TextFont( kFontIDMonaco
) ;
273 // Find the widest line
274 for(int i
= 0; i
< GetCount(); i
++) {
275 wxString
str(GetString(i
));
279 ::GetThemeTextDimensions( wxMacCFStringHolder( str
, m_font
.GetEncoding() ) ,
280 kThemeCurrentPortFont
,
287 wLine
= ::TextWidth( str
.c_str() , 0 , str
.Length() ) ;
289 lbWidth
= wxMax(lbWidth
, wLine
);
291 // Add room for the popup arrow
292 lbWidth
+= 2 * lbHeight
;
293 // And just a bit more
294 int cx
= ::TextWidth( "X" , 0 , 1 ) ;
298 return wxSize(lbWidth
, lbHeight
);