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
)
60 UMAAppendMenuItem(MAC_WXHMENU( m_macPopUpMenuHandle
) , item
);
61 m_strings
.Add( item
) ;
63 int index
= m_strings
.GetCount() - 1 ;
64 DoSetItemClientData( index
, NULL
) ;
65 SetControl32BitMaximum( (ControlHandle
) m_macControl
, GetCount()) ;
68 void wxChoice::Delete(int n
)
70 wxCHECK_RET( n
< GetCount(), wxT("invalid item index in wxChoice::Delete") );
71 if ( HasClientObjectData() )
73 delete GetClientObject(n
);
75 ::DeleteMenuItem( MAC_WXHMENU(m_macPopUpMenuHandle
) , n
+ 1) ;
76 m_strings
.Remove( n
) ;
77 m_datas
.RemoveAt( n
) ;
78 SetControl32BitMaximum( (ControlHandle
) m_macControl
, GetCount()) ;
80 void wxChoice::Clear()
83 for ( int i
= 0 ; i
< GetCount() ; i
++ )
85 ::DeleteMenuItem( MAC_WXHMENU(m_macPopUpMenuHandle
) , 1 ) ;
89 SetControl32BitMaximum( (ControlHandle
) m_macControl
, 0 ) ;
91 void wxChoice::FreeData()
93 if ( HasClientObjectData() )
95 size_t count
= GetCount();
96 for ( size_t n
= 0; n
< count
; n
++ )
98 delete GetClientObject(n
);
102 // ----------------------------------------------------------------------------
104 // ----------------------------------------------------------------------------
105 int wxChoice::GetSelection() const
107 return GetControl32BitValue( (ControlHandle
) m_macControl
) -1 ;
109 void wxChoice::SetSelection(int n
)
111 SetControl32BitValue( (ControlHandle
) m_macControl
, n
+ 1 ) ;
113 // ----------------------------------------------------------------------------
114 // string list functions
115 // ----------------------------------------------------------------------------
116 int wxChoice::GetCount() const
118 return m_strings
.GetCount() ;
120 int wxChoice::FindString(const wxString
& s
) const
122 for( int i
= 0 ; i
< GetCount() ; i
++ )
124 if ( GetString( i
).IsSameAs(s
, FALSE
) )
129 void wxChoice::SetString(int n
, const wxString
& s
)
131 wxFAIL_MSG(wxT("wxChoice::SetString() not yet implemented"));
132 #if 0 // should do this, but no Insert() so far
138 wxString
wxChoice::GetString(int n
) const
140 return m_strings
[n
] ;
142 // ----------------------------------------------------------------------------
144 // ----------------------------------------------------------------------------
145 void wxChoice::DoSetItemClientData( int n
, void* clientData
)
147 wxCHECK_RET( n
>= 0 && (size_t)n
< m_datas
.GetCount(),
148 "invalid index in wxChoice::SetClientData" );
150 m_datas
[n
] = (char*) clientData
;
152 void *wxChoice::DoGetItemClientData(int n
) const
154 wxCHECK_MSG( n
>= 0 && (size_t)n
< m_datas
.GetCount(), NULL
,
155 "invalid index in wxChoice::GetClientData" );
156 return (void *)m_datas
[n
];
158 void wxChoice::DoSetItemClientObject( int n
, wxClientData
* clientData
)
160 DoSetItemClientData(n
, clientData
);
162 wxClientData
* wxChoice::DoGetItemClientObject( int n
) const
164 return (wxClientData
*)DoGetItemClientData(n
);
166 void wxChoice::MacHandleControlClick( WXWidget control
, wxInt16 controlpart
)
168 wxCommandEvent
event(wxEVT_COMMAND_CHOICE_SELECTED
, m_windowId
);
169 int n
= GetSelection();
170 // actually n should be made sure by the os to be a valid selection, but ...
174 event
.SetString(GetStringSelection());
175 event
.SetEventObject(this);
176 if ( HasClientObjectData() )
177 event
.SetClientObject( GetClientObject(n
) );
178 else if ( HasClientUntypedData() )
179 event
.SetClientData( GetClientData(n
) );
180 ProcessCommand(event
);
183 wxSize
wxChoice::DoGetBestSize() const
185 int lbWidth
= 100; // some defaults
190 GetThemeMetric(kThemeMetricPopupButtonHeight
, &metric
);
194 wxMacPortStateHelper
st( UMAGetWindowPort( (WindowRef
) MacGetRootWindow() ) ) ;
196 wxFontRefData
* font
= (wxFontRefData
*) m_font
.GetRefData() ;
199 ::TextFont( font
->m_macFontNum
) ;
200 ::TextSize( short(font
->m_macFontSize
) ) ;
201 ::TextFace( font
->m_macFontStyle
) ;
205 ::TextFont( kFontIDMonaco
) ;
209 // Find the widest line
210 for(int i
= 0; i
< GetCount(); i
++) {
211 wxString
str(GetString(i
));
212 wLine
= ::TextWidth( str
.c_str() , 0 , str
.Length() ) ;
213 lbWidth
= wxMax(lbWidth
, wLine
);
215 // Add room for the popup arrow
216 lbWidth
+= 2 * lbHeight
;
217 // And just a bit more
219 int cx
= ::TextWidth( "X" , 0 , 1 ) ;
223 return wxSize(lbWidth
, lbHeight
);