1 /////////////////////////////////////////////////////////////////////////////
8 // Copyright: (c) AUTHOR
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "choice.h"
18 #include "wx/choice.h"
20 #include "wx/mac/uma.h"
22 #if !USE_SHARED_LIBRARY
23 IMPLEMENT_DYNAMIC_CLASS(wxChoice
, wxControl
)
26 extern MenuHandle
NewUniqueMenu() ;
30 // DeleteMenu( m_macPopUpMenuId ) ;
31 // DisposeMenu( m_macPopUpMenuHandle ) ;
34 bool wxChoice::Create(wxWindow
*parent
, wxWindowID id
,
37 int n
, const wxString choices
[],
39 const wxValidator
& validator
,
46 MacPreControlCreate( parent
, id
, "" , pos
, size
,style
, validator
, name
, &bounds
, title
) ;
48 m_macControl
= ::NewControl( MAC_WXHWND(parent
->MacGetRootWindow()) , &bounds
, title
, false , 0 , -12345 , 0 ,
49 kControlPopupButtonProc
+ kControlPopupFixedWidthVariant
, (long) this ) ;
51 m_macPopUpMenuHandle
= NewUniqueMenu() ;
52 SetControlData( (ControlHandle
) m_macControl
, kControlNoPart
, kControlPopupButtonMenuHandleTag
, sizeof( MenuHandle
) , (char*) &m_macPopUpMenuHandle
) ;
53 SetControl32BitMinimum( (ControlHandle
) m_macControl
, 0 ) ;
54 SetControl32BitMaximum( (ControlHandle
) m_macControl
, 0) ;
56 SetControl32BitValue( (ControlHandle
) m_macControl
, 1 ) ;
58 MacPostControlCreate() ;
60 for ( int i
= 0; i
< n
; i
++ )
67 // ----------------------------------------------------------------------------
68 // adding/deleting items to/from the list
69 // ----------------------------------------------------------------------------
71 int wxChoice::DoAppend(const wxString
& item
)
74 wxMenuItem::MacBuildMenuString( label
, NULL
, NULL
, item
,false);
75 AppendMenu( MAC_WXHMENU( m_macPopUpMenuHandle
) , label
) ;
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") );
88 if ( HasClientObjectData() )
90 delete GetClientObject(n
);
93 ::DeleteMenuItem( MAC_WXHMENU(m_macPopUpMenuHandle
) , n
+ 1) ;
94 m_strings
.Remove( n
) ;
95 m_datas
.RemoveAt( n
) ;
96 SetControl32BitMaximum( (ControlHandle
) m_macControl
, GetCount()) ;
99 void wxChoice::Clear()
103 for ( int i
= 0 ; i
< GetCount() ; i
++ )
105 ::DeleteMenuItem( MAC_WXHMENU(m_macPopUpMenuHandle
) , 1 ) ;
109 SetControl32BitMaximum( (ControlHandle
) m_macControl
, 0 ) ;
112 void wxChoice::FreeData()
114 if ( HasClientObjectData() )
116 size_t count
= GetCount();
117 for ( size_t n
= 0; n
< count
; n
++ )
119 delete GetClientObject(n
);
124 // ----------------------------------------------------------------------------
126 // ----------------------------------------------------------------------------
128 int wxChoice::GetSelection() const
130 return GetControl32BitValue( (ControlHandle
) m_macControl
) -1 ;
133 void wxChoice::SetSelection(int n
)
135 SetControl32BitValue( (ControlHandle
) m_macControl
, n
+ 1 ) ;
138 // ----------------------------------------------------------------------------
139 // string list functions
140 // ----------------------------------------------------------------------------
142 int wxChoice::GetCount() const
144 return m_strings
.GetCount() ;
147 int wxChoice::FindString(const wxString
& s
) const
149 for( int i
= 0 ; i
< GetCount() ; i
++ )
151 if ( GetString( i
).IsSameAs(s
, FALSE
) )
157 void wxChoice::SetString(int n
, const wxString
& s
)
159 wxFAIL_MSG(wxT("wxChoice::SetString() not yet implemented"));
161 #if 0 // should do this, but no Insert() so far
168 wxString
wxChoice::GetString(int n
) const
170 return m_strings
[n
] ;
173 // ----------------------------------------------------------------------------
175 // ----------------------------------------------------------------------------
177 void wxChoice::DoSetItemClientData( int n
, void* clientData
)
179 wxCHECK_RET( n
>= 0 && (size_t)n
< m_datas
.GetCount(),
180 "invalid index in wxChoice::SetClientData" );
182 m_datas
[n
] = (char*) clientData
;
185 void *wxChoice::DoGetItemClientData(int n
) const
187 wxCHECK_MSG( n
>= 0 && (size_t)n
< m_datas
.GetCount(), NULL
,
188 "invalid index in wxChoice::GetClientData" );
190 return (void *)m_datas
[n
];
193 void wxChoice::DoSetItemClientObject( int n
, wxClientData
* clientData
)
195 DoSetItemClientData(n
, clientData
);
198 wxClientData
* wxChoice::DoGetItemClientObject( int n
) const
200 return (wxClientData
*)DoGetItemClientData(n
);
203 void wxChoice::MacHandleControlClick( WXWidget control
, wxInt16 controlpart
)
205 wxCommandEvent
event(wxEVT_COMMAND_CHOICE_SELECTED
, m_windowId
);
206 event
.SetInt(GetSelection());
207 event
.SetEventObject(this);
208 event
.SetString(GetStringSelection());
209 ProcessCommand(event
);
212 wxSize
wxChoice::DoGetBestSize() const
214 // TODO should modify this to take into account string length ala wxGTK
215 return wxSize(100,20);
219 void wxChoice::Command(wxCommandEvent & event)
221 SetSelection (event.GetInt());
222 ProcessCommand (event);