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
) ;
70 verify_noerr ( CreatePopupButtonControl( MAC_WXHWND(parent
->MacGetTopLevelWindowRef()) , &bounds
, CFSTR("") ,
71 -12345 , false /* no variable width */ , 0 , 0 , 0 , (ControlRef
*) &m_macControl
) ) ;
73 m_macPopUpMenuHandle
= NewUniqueMenu() ;
74 SetControlData( (ControlRef
) m_macControl
, kControlNoPart
, kControlPopupButtonMenuHandleTag
, sizeof( MenuHandle
) , (char*) &m_macPopUpMenuHandle
) ;
75 SetControl32BitMinimum( (ControlRef
) m_macControl
, 0 ) ;
76 SetControl32BitMaximum( (ControlRef
) m_macControl
, 0) ;
78 SetControl32BitValue( (ControlRef
) m_macControl
, 1 ) ;
79 MacPostControlCreate(pos
,size
) ;
81 for ( int i
= 0; i
< n
; i
++ )
85 SetBestSize(size
); // Needed because it is a wxControlWithItems
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( (ControlRef
) m_macControl
, GetCount()) ;
103 int wxChoice::DoInsert(const wxString
& item
, int pos
)
105 wxCHECK_MSG(!(GetWindowStyle() & wxCB_SORT
), -1, wxT("can't insert into sorted list"));
106 wxCHECK_MSG((pos
>=0) && (pos
<=GetCount()), -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( (ControlRef
) m_macControl
, pos
) ;
119 void wxChoice::Delete(int n
)
121 wxCHECK_RET( n
< GetCount(), 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( (ControlRef
) m_macControl
, GetCount()) ;
132 void wxChoice::Clear()
135 for ( int i
= 0 ; i
< GetCount() ; i
++ )
137 ::DeleteMenuItem( MAC_WXHMENU(m_macPopUpMenuHandle
) , 1 ) ;
141 SetControl32BitMaximum( (ControlRef
) m_macControl
, 0 ) ;
144 void wxChoice::FreeData()
146 if ( HasClientObjectData() )
148 size_t count
= GetCount();
149 for ( size_t n
= 0; n
< count
; n
++ )
151 delete GetClientObject(n
);
156 // ----------------------------------------------------------------------------
158 // ----------------------------------------------------------------------------
159 int wxChoice::GetSelection() const
161 return GetControl32BitValue( (ControlRef
) m_macControl
) -1 ;
164 void wxChoice::SetSelection(int n
)
166 SetControl32BitValue( (ControlRef
) m_macControl
, n
+ 1 ) ;
169 // ----------------------------------------------------------------------------
170 // string list functions
171 // ----------------------------------------------------------------------------
173 int wxChoice::GetCount() const
175 return m_strings
.GetCount() ;
178 int wxChoice::FindString(const wxString
& s
) const
180 for( int i
= 0 ; i
< GetCount() ; i
++ )
182 if ( GetString( i
).IsSameAs(s
, FALSE
) )
188 void wxChoice::SetString(int n
, const wxString
& s
)
190 wxFAIL_MSG(wxT("wxChoice::SetString() not yet implemented"));
191 #if 0 // should do this, but no Insert() so far
197 wxString
wxChoice::GetString(int n
) const
199 wxCHECK_MSG( n
>= 0 && (size_t)n
< m_strings
.GetCount(), _T(""),
200 _T("wxChoice::GetString(): invalid index") );
202 return m_strings
[n
] ;
205 // ----------------------------------------------------------------------------
207 // ----------------------------------------------------------------------------
208 void wxChoice::DoSetItemClientData( int n
, void* clientData
)
210 wxCHECK_RET( n
>= 0 && (size_t)n
< m_datas
.GetCount(),
211 wxT("invalid index in wxChoice::SetClientData") );
213 m_datas
[n
] = (char*) clientData
;
216 void *wxChoice::DoGetItemClientData(int n
) const
218 wxCHECK_MSG( n
>= 0 && (size_t)n
< m_datas
.GetCount(), NULL
,
219 wxT("invalid index in wxChoice::GetClientData") );
220 return (void *)m_datas
[n
];
223 void wxChoice::DoSetItemClientObject( int n
, wxClientData
* clientData
)
225 DoSetItemClientData(n
, clientData
);
228 wxClientData
* wxChoice::DoGetItemClientObject( int n
) const
230 return (wxClientData
*)DoGetItemClientData(n
);
233 wxInt32
wxChoice::MacControlHit(WXEVENTHANDLERREF
WXUNUSED(handler
) , WXEVENTREF
WXUNUSED(event
) )
235 wxCommandEvent
event(wxEVT_COMMAND_CHOICE_SELECTED
, m_windowId
);
236 int n
= GetSelection();
237 // actually n should be made sure by the os to be a valid selection, but ...
241 event
.SetString(GetStringSelection());
242 event
.SetEventObject(this);
243 if ( HasClientObjectData() )
244 event
.SetClientObject( GetClientObject(n
) );
245 else if ( HasClientUntypedData() )
246 event
.SetClientData( GetClientData(n
) );
247 ProcessCommand(event
);
252 wxSize
wxChoice::DoGetBestSize() const
254 int lbWidth
= GetCount() > 0 ? 20 : 100; // some defaults
259 GetThemeMetric(kThemeMetricPopupButtonHeight
, &metric
);
263 wxMacPortStateHelper
st( UMAGetWindowPort( (WindowRef
) MacGetTopLevelWindowRef() ) ) ;
266 ::TextFont( m_font
.MacGetFontNum() ) ;
267 ::TextSize( m_font
.MacGetFontSize() ) ;
268 ::TextFace( m_font
.MacGetFontStyle() ) ;
272 ::TextFont( kFontIDMonaco
) ;
276 // Find the widest line
277 for(int i
= 0; i
< GetCount(); i
++) {
278 wxString
str(GetString(i
));
282 ::GetThemeTextDimensions( wxMacCFStringHolder( str
, m_font
.GetEncoding() ) ,
283 kThemeCurrentPortFont
,
290 wLine
= ::TextWidth( str
.c_str() , 0 , str
.Length() ) ;
292 lbWidth
= wxMax(lbWidth
, wLine
);
294 // Add room for the popup arrow
295 lbWidth
+= 2 * lbHeight
;
296 // And just a bit more
297 int cx
= ::TextWidth( "X" , 0 , 1 ) ;
301 return wxSize(lbWidth
, lbHeight
);