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 if ( HasClientObjectData() )
26 size_t i
, max
= GetCount();
28 for ( i
= 0; i
< max
; ++i
)
29 delete GetClientObject(i
);
32 // DeleteMenu( m_macPopUpMenuId ) ;
33 // DisposeMenu( m_macPopUpMenuHandle ) ;
35 bool wxChoice::Create(wxWindow
*parent
, wxWindowID id
,
38 int n
, const wxString choices
[],
40 const wxValidator
& validator
,
46 MacPreControlCreate( parent
, id
, "" , pos
, size
,style
, validator
, name
, &bounds
, title
) ;
47 m_macControl
= ::NewControl( MAC_WXHWND(parent
->MacGetRootWindow()) , &bounds
, title
, false , 0 , -12345 , 0 ,
48 kControlPopupButtonProc
+ kControlPopupFixedWidthVariant
, (long) this ) ;
50 m_macPopUpMenuHandle
= NewUniqueMenu() ;
51 SetControlData( (ControlHandle
) m_macControl
, kControlNoPart
, kControlPopupButtonMenuHandleTag
, sizeof( MenuHandle
) , (char*) &m_macPopUpMenuHandle
) ;
52 SetControl32BitMinimum( (ControlHandle
) m_macControl
, 0 ) ;
53 SetControl32BitMaximum( (ControlHandle
) m_macControl
, 0) ;
55 SetControl32BitValue( (ControlHandle
) m_macControl
, 1 ) ;
56 MacPostControlCreate() ;
57 for ( int i
= 0; i
< n
; i
++ )
63 // ----------------------------------------------------------------------------
64 // adding/deleting items to/from the list
65 // ----------------------------------------------------------------------------
66 int wxChoice::DoAppend(const wxString
& item
)
68 UMAAppendMenuItem(MAC_WXHMENU( m_macPopUpMenuHandle
) , item
);
69 m_strings
.Add( item
) ;
71 int index
= m_strings
.GetCount() - 1 ;
72 DoSetItemClientData( index
, NULL
) ;
73 SetControl32BitMaximum( (ControlHandle
) m_macControl
, GetCount()) ;
76 void wxChoice::Delete(int n
)
78 wxCHECK_RET( n
< GetCount(), wxT("invalid item index in wxChoice::Delete") );
79 if ( HasClientObjectData() )
81 delete GetClientObject(n
);
83 ::DeleteMenuItem( MAC_WXHMENU(m_macPopUpMenuHandle
) , n
+ 1) ;
84 m_strings
.Remove( n
) ;
85 m_datas
.RemoveAt( n
) ;
86 SetControl32BitMaximum( (ControlHandle
) m_macControl
, GetCount()) ;
88 void wxChoice::Clear()
91 for ( int i
= 0 ; i
< GetCount() ; i
++ )
93 ::DeleteMenuItem( MAC_WXHMENU(m_macPopUpMenuHandle
) , 1 ) ;
97 SetControl32BitMaximum( (ControlHandle
) m_macControl
, 0 ) ;
99 void wxChoice::FreeData()
101 if ( HasClientObjectData() )
103 size_t count
= GetCount();
104 for ( size_t n
= 0; n
< count
; n
++ )
106 delete GetClientObject(n
);
110 // ----------------------------------------------------------------------------
112 // ----------------------------------------------------------------------------
113 int wxChoice::GetSelection() const
115 return GetControl32BitValue( (ControlHandle
) m_macControl
) -1 ;
117 void wxChoice::SetSelection(int n
)
119 SetControl32BitValue( (ControlHandle
) m_macControl
, n
+ 1 ) ;
121 // ----------------------------------------------------------------------------
122 // string list functions
123 // ----------------------------------------------------------------------------
124 int wxChoice::GetCount() const
126 return m_strings
.GetCount() ;
128 int wxChoice::FindString(const wxString
& s
) const
130 for( int i
= 0 ; i
< GetCount() ; i
++ )
132 if ( GetString( i
).IsSameAs(s
, FALSE
) )
137 void wxChoice::SetString(int n
, const wxString
& s
)
139 wxFAIL_MSG(wxT("wxChoice::SetString() not yet implemented"));
140 #if 0 // should do this, but no Insert() so far
146 wxString
wxChoice::GetString(int n
) const
148 return m_strings
[n
] ;
150 // ----------------------------------------------------------------------------
152 // ----------------------------------------------------------------------------
153 void wxChoice::DoSetItemClientData( int n
, void* clientData
)
155 wxCHECK_RET( n
>= 0 && (size_t)n
< m_datas
.GetCount(),
156 "invalid index in wxChoice::SetClientData" );
158 m_datas
[n
] = (char*) clientData
;
160 void *wxChoice::DoGetItemClientData(int n
) const
162 wxCHECK_MSG( n
>= 0 && (size_t)n
< m_datas
.GetCount(), NULL
,
163 "invalid index in wxChoice::GetClientData" );
164 return (void *)m_datas
[n
];
166 void wxChoice::DoSetItemClientObject( int n
, wxClientData
* clientData
)
168 DoSetItemClientData(n
, clientData
);
170 wxClientData
* wxChoice::DoGetItemClientObject( int n
) const
172 return (wxClientData
*)DoGetItemClientData(n
);
174 void wxChoice::MacHandleControlClick( WXWidget control
, wxInt16 controlpart
)
176 wxCommandEvent
event(wxEVT_COMMAND_CHOICE_SELECTED
, m_windowId
);
177 int n
= GetSelection();
178 // actually n should be made sure by the os to be a valid selection, but ...
182 event
.SetString(GetStringSelection());
183 event
.SetEventObject(this);
184 if ( HasClientObjectData() )
185 event
.SetClientObject( GetClientObject(n
) );
186 else if ( HasClientUntypedData() )
187 event
.SetClientData( GetClientData(n
) );
188 ProcessCommand(event
);
191 wxSize
wxChoice::DoGetBestSize() const
193 int lbWidth
= 100; // some defaults
198 GetThemeMetric(kThemeMetricPopupButtonHeight
, &metric
);
202 wxMacPortStateHelper
st( UMAGetWindowPort( (WindowRef
) MacGetRootWindow() ) ) ;
204 wxFontRefData
* font
= (wxFontRefData
*) m_font
.GetRefData() ;
207 ::TextFont( font
->m_macFontNum
) ;
208 ::TextSize( short(font
->m_macFontSize
) ) ;
209 ::TextFace( font
->m_macFontStyle
) ;
213 ::TextFont( kFontIDMonaco
) ;
217 // Find the widest line
218 for(int i
= 0; i
< GetCount(); i
++) {
219 wxString
str(GetString(i
));
220 wLine
= ::TextWidth( str
.c_str() , 0 , str
.Length() ) ;
221 lbWidth
= wxMax(lbWidth
, wLine
);
223 // Add room for the popup arrow
224 lbWidth
+= 2 * lbHeight
;
225 // And just a bit more
227 int cx
= ::TextWidth( "X" , 0 , 1 ) ;
231 return wxSize(lbWidth
, lbHeight
);