1 /////////////////////////////////////////////////////////////////////////////
8 // Copyright: (c) AUTHOR
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #pragma implementation "choice.h"
15 #include "wx/choice.h"
17 #include "wx/mac/uma.h"
18 #if !USE_SHARED_LIBRARY
19 IMPLEMENT_DYNAMIC_CLASS(wxChoice
, wxControl
)
21 extern MenuHandle
NewUniqueMenu() ;
24 // DeleteMenu( m_macPopUpMenuId ) ;
25 // DisposeMenu( m_macPopUpMenuHandle ) ;
27 bool wxChoice::Create(wxWindow
*parent
, wxWindowID id
,
30 int n
, const wxString choices
[],
32 const wxValidator
& validator
,
38 MacPreControlCreate( parent
, id
, "" , pos
, size
,style
, validator
, name
, &bounds
, title
) ;
39 m_macControl
= ::NewControl( MAC_WXHWND(parent
->MacGetRootWindow()) , &bounds
, title
, false , 0 , -12345 , 0 ,
40 kControlPopupButtonProc
+ kControlPopupFixedWidthVariant
, (long) this ) ;
42 m_macPopUpMenuHandle
= NewUniqueMenu() ;
43 SetControlData( (ControlHandle
) m_macControl
, kControlNoPart
, kControlPopupButtonMenuHandleTag
, sizeof( MenuHandle
) , (char*) &m_macPopUpMenuHandle
) ;
44 SetControl32BitMinimum( (ControlHandle
) m_macControl
, 0 ) ;
45 SetControl32BitMaximum( (ControlHandle
) m_macControl
, 0) ;
47 SetControl32BitValue( (ControlHandle
) m_macControl
, 1 ) ;
48 MacPostControlCreate() ;
49 for ( int i
= 0; i
< n
; i
++ )
55 // ----------------------------------------------------------------------------
56 // adding/deleting items to/from the list
57 // ----------------------------------------------------------------------------
58 int wxChoice::DoAppend(const wxString
& item
)
61 wxMenuItem::MacBuildMenuString( label
, NULL
, NULL
, item
,false);
62 MacAppendMenu(MAC_WXHMENU( m_macPopUpMenuHandle
) , "\pA");
63 SetMenuItemText(MAC_WXHMENU( m_macPopUpMenuHandle
) ,
64 (SInt16
) ::CountMenuItems(MAC_WXHMENU( m_macPopUpMenuHandle
) ), label
);
65 // was AppendMenu( MAC_WXHMENU( m_macPopUpMenuHandle ) , label ) ;
66 m_strings
.Add( item
) ;
68 int index
= m_strings
.GetCount() - 1 ;
69 DoSetItemClientData( index
, NULL
) ;
70 SetControl32BitMaximum( (ControlHandle
) m_macControl
, GetCount()) ;
73 void wxChoice::Delete(int n
)
75 wxCHECK_RET( n
< GetCount(), wxT("invalid item index in wxChoice::Delete") );
76 if ( HasClientObjectData() )
78 delete GetClientObject(n
);
80 ::DeleteMenuItem( MAC_WXHMENU(m_macPopUpMenuHandle
) , n
+ 1) ;
81 m_strings
.Remove( n
) ;
82 m_datas
.RemoveAt( n
) ;
83 SetControl32BitMaximum( (ControlHandle
) m_macControl
, GetCount()) ;
85 void wxChoice::Clear()
88 for ( int i
= 0 ; i
< GetCount() ; i
++ )
90 ::DeleteMenuItem( MAC_WXHMENU(m_macPopUpMenuHandle
) , 1 ) ;
94 SetControl32BitMaximum( (ControlHandle
) m_macControl
, 0 ) ;
96 void wxChoice::FreeData()
98 if ( HasClientObjectData() )
100 size_t count
= GetCount();
101 for ( size_t n
= 0; n
< count
; n
++ )
103 delete GetClientObject(n
);
107 // ----------------------------------------------------------------------------
109 // ----------------------------------------------------------------------------
110 int wxChoice::GetSelection() const
112 return GetControl32BitValue( (ControlHandle
) m_macControl
) -1 ;
114 void wxChoice::SetSelection(int n
)
116 SetControl32BitValue( (ControlHandle
) m_macControl
, n
+ 1 ) ;
118 // ----------------------------------------------------------------------------
119 // string list functions
120 // ----------------------------------------------------------------------------
121 int wxChoice::GetCount() const
123 return m_strings
.GetCount() ;
125 int wxChoice::FindString(const wxString
& s
) const
127 for( int i
= 0 ; i
< GetCount() ; i
++ )
129 if ( GetString( i
).IsSameAs(s
, FALSE
) )
134 void wxChoice::SetString(int n
, const wxString
& s
)
136 wxFAIL_MSG(wxT("wxChoice::SetString() not yet implemented"));
137 #if 0 // should do this, but no Insert() so far
143 wxString
wxChoice::GetString(int n
) const
145 return m_strings
[n
] ;
147 // ----------------------------------------------------------------------------
149 // ----------------------------------------------------------------------------
150 void wxChoice::DoSetItemClientData( int n
, void* clientData
)
152 wxCHECK_RET( n
>= 0 && (size_t)n
< m_datas
.GetCount(),
153 "invalid index in wxChoice::SetClientData" );
155 m_datas
[n
] = (char*) clientData
;
157 void *wxChoice::DoGetItemClientData(int n
) const
159 wxCHECK_MSG( n
>= 0 && (size_t)n
< m_datas
.GetCount(), NULL
,
160 "invalid index in wxChoice::GetClientData" );
161 return (void *)m_datas
[n
];
163 void wxChoice::DoSetItemClientObject( int n
, wxClientData
* clientData
)
165 DoSetItemClientData(n
, clientData
);
167 wxClientData
* wxChoice::DoGetItemClientObject( int n
) const
169 return (wxClientData
*)DoGetItemClientData(n
);
171 void wxChoice::MacHandleControlClick( WXWidget control
, wxInt16 controlpart
)
173 wxCommandEvent
event(wxEVT_COMMAND_CHOICE_SELECTED
, m_windowId
);
174 int n
= GetSelection();
175 // actually n should be made sure by the os to be a valid selection, but ...
179 event
.SetString(GetStringSelection());
180 event
.SetEventObject(this);
181 if ( HasClientObjectData() )
182 event
.SetClientObject( GetClientObject(n
) );
183 else if ( HasClientUntypedData() )
184 event
.SetClientData( GetClientData(n
) );
185 ProcessCommand(event
);
188 wxSize
wxChoice::DoGetBestSize() const
190 int lbWidth
= 100; // some defaults
195 GetThemeMetric(kThemeMetricPopupButtonHeight
, &metric
);
199 wxMacPortStateHelper
st( UMAGetWindowPort( (WindowRef
) MacGetRootWindow() ) ) ;
201 wxFontRefData
* font
= (wxFontRefData
*) m_font
.GetRefData() ;
204 ::TextFont( font
->m_macFontNum
) ;
205 ::TextSize( short(font
->m_macFontSize
) ) ;
206 ::TextFace( font
->m_macFontStyle
) ;
210 ::TextFont( kFontIDMonaco
) ;
214 // Find the widest line
215 for(int i
= 0; i
< GetCount(); i
++) {
216 wxString
str(GetString(i
));
217 wLine
= ::TextWidth( str
.c_str() , 0 , str
.Length() ) ;
218 lbWidth
= wxMax(lbWidth
, wLine
);
220 // Add room for the popup arrow
221 lbWidth
+= 2 * lbHeight
;
222 // And just a bit more
224 int cx
= ::TextWidth( "X" , 0 , 1 ) ;
228 return wxSize(lbWidth
, lbHeight
);