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 int n
, const wxString choices
[],
46 const wxValidator
& validator
,
52 MacPreControlCreate( parent
, id
, "" , pos
, size
,style
, validator
, name
, &bounds
, title
) ;
53 m_macControl
= ::NewControl( MAC_WXHWND(parent
->MacGetRootWindow()) , &bounds
, title
, false , 0 , -12345 , 0 ,
54 kControlPopupButtonProc
+ kControlPopupFixedWidthVariant
, (long) this ) ;
56 m_macPopUpMenuHandle
= NewUniqueMenu() ;
57 SetControlData( (ControlHandle
) m_macControl
, kControlNoPart
, kControlPopupButtonMenuHandleTag
, sizeof( MenuHandle
) , (char*) &m_macPopUpMenuHandle
) ;
58 SetControl32BitMinimum( (ControlHandle
) m_macControl
, 0 ) ;
59 SetControl32BitMaximum( (ControlHandle
) m_macControl
, 0) ;
61 SetControl32BitValue( (ControlHandle
) m_macControl
, 1 ) ;
62 MacPostControlCreate() ;
63 for ( int i
= 0; i
< n
; i
++ )
70 // ----------------------------------------------------------------------------
71 // adding/deleting items to/from the list
72 // ----------------------------------------------------------------------------
73 int wxChoice::DoAppend(const wxString
& item
)
75 UMAAppendMenuItem(MAC_WXHMENU( m_macPopUpMenuHandle
) , item
);
76 m_strings
.Add( item
) ;
78 int index
= m_strings
.GetCount() - 1 ;
79 DoSetItemClientData( index
, NULL
) ;
80 SetControl32BitMaximum( (ControlHandle
) m_macControl
, GetCount()) ;
84 void wxChoice::Delete(int n
)
86 wxCHECK_RET( n
< GetCount(), wxT("invalid item index in wxChoice::Delete") );
87 if ( HasClientObjectData() )
89 delete GetClientObject(n
);
91 ::DeleteMenuItem( MAC_WXHMENU(m_macPopUpMenuHandle
) , n
+ 1) ;
92 m_strings
.Remove( n
) ;
93 m_datas
.RemoveAt( n
) ;
94 SetControl32BitMaximum( (ControlHandle
) m_macControl
, GetCount()) ;
97 void wxChoice::Clear()
100 for ( int i
= 0 ; i
< GetCount() ; i
++ )
102 ::DeleteMenuItem( MAC_WXHMENU(m_macPopUpMenuHandle
) , 1 ) ;
106 SetControl32BitMaximum( (ControlHandle
) m_macControl
, 0 ) ;
109 void wxChoice::FreeData()
111 if ( HasClientObjectData() )
113 size_t count
= GetCount();
114 for ( size_t n
= 0; n
< count
; n
++ )
116 delete GetClientObject(n
);
121 // ----------------------------------------------------------------------------
123 // ----------------------------------------------------------------------------
124 int wxChoice::GetSelection() const
126 return GetControl32BitValue( (ControlHandle
) m_macControl
) -1 ;
129 void wxChoice::SetSelection(int n
)
131 SetControl32BitValue( (ControlHandle
) m_macControl
, n
+ 1 ) ;
134 // ----------------------------------------------------------------------------
135 // string list functions
136 // ----------------------------------------------------------------------------
138 int wxChoice::GetCount() const
140 return m_strings
.GetCount() ;
143 int wxChoice::FindString(const wxString
& s
) const
145 for( int i
= 0 ; i
< GetCount() ; i
++ )
147 if ( GetString( i
).IsSameAs(s
, FALSE
) )
153 void wxChoice::SetString(int n
, const wxString
& s
)
155 wxFAIL_MSG(wxT("wxChoice::SetString() not yet implemented"));
156 #if 0 // should do this, but no Insert() so far
162 wxString
wxChoice::GetString(int n
) const
164 return m_strings
[n
] ;
166 // ----------------------------------------------------------------------------
168 // ----------------------------------------------------------------------------
169 void wxChoice::DoSetItemClientData( int n
, void* clientData
)
171 wxCHECK_RET( n
>= 0 && (size_t)n
< m_datas
.GetCount(),
172 "invalid index in wxChoice::SetClientData" );
174 m_datas
[n
] = (char*) clientData
;
177 void *wxChoice::DoGetItemClientData(int n
) const
179 wxCHECK_MSG( n
>= 0 && (size_t)n
< m_datas
.GetCount(), NULL
,
180 "invalid index in wxChoice::GetClientData" );
181 return (void *)m_datas
[n
];
184 void wxChoice::DoSetItemClientObject( int n
, wxClientData
* clientData
)
186 DoSetItemClientData(n
, clientData
);
189 wxClientData
* wxChoice::DoGetItemClientObject( int n
) const
191 return (wxClientData
*)DoGetItemClientData(n
);
194 void wxChoice::MacHandleControlClick( WXWidget control
, wxInt16 controlpart
)
196 wxCommandEvent
event(wxEVT_COMMAND_CHOICE_SELECTED
, m_windowId
);
197 int n
= GetSelection();
198 // actually n should be made sure by the os to be a valid selection, but ...
202 event
.SetString(GetStringSelection());
203 event
.SetEventObject(this);
204 if ( HasClientObjectData() )
205 event
.SetClientObject( GetClientObject(n
) );
206 else if ( HasClientUntypedData() )
207 event
.SetClientData( GetClientData(n
) );
208 ProcessCommand(event
);
212 wxSize
wxChoice::DoGetBestSize() const
214 int lbWidth
= GetCount() > 0 ? 20 : 100; // some defaults
219 GetThemeMetric(kThemeMetricPopupButtonHeight
, &metric
);
223 wxMacPortStateHelper
st( UMAGetWindowPort( (WindowRef
) MacGetRootWindow() ) ) ;
225 wxFontRefData
* font
= (wxFontRefData
*) m_font
.GetRefData() ;
228 ::TextFont( font
->m_macFontNum
) ;
229 ::TextSize( short(font
->m_macFontSize
) ) ;
230 ::TextFace( font
->m_macFontStyle
) ;
234 ::TextFont( kFontIDMonaco
) ;
238 // Find the widest line
239 for(int i
= 0; i
< GetCount(); i
++) {
240 wxString
str(GetString(i
));
241 wLine
= ::TextWidth( str
.c_str() , 0 , str
.Length() ) ;
242 lbWidth
= wxMax(lbWidth
, wLine
);
244 // Add room for the popup arrow
245 lbWidth
+= 2 * lbHeight
;
246 // And just a bit more
248 int cx
= ::TextWidth( "X" , 0 , 1 ) ;
252 return wxSize(lbWidth
, lbHeight
);