1 /////////////////////////////////////////////////////////////////////////////
8 // Copyright: (c) AUTHOR
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "choice.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/choice.h"
19 #include "wx/mac/uma.h"
21 #if !USE_SHARED_LIBRARY
22 IMPLEMENT_DYNAMIC_CLASS(wxChoice
, wxControl
)
25 short nextMenuId
= 100 ; // wxMenu takes the lower ids
29 // DeleteMenu( m_macPopUpMenuId ) ;
30 // DisposeMenu( m_macPopUpMenuHandle ) ;
33 bool wxChoice::Create(wxWindow
*parent
, wxWindowID id
,
36 int n
, const wxString choices
[],
38 const wxValidator
& validator
,
45 MacPreControlCreate( parent
, id
, "" , pos
, size
,style
, validator
, name
, &bounds
, title
) ;
47 m_macControl
= UMANewControl( parent
->GetMacRootWindow() , &bounds
, title
, false , 0 , -12345 , 0 ,
48 kControlPopupButtonProc
+ kControlPopupFixedWidthVariant
, (long) this ) ;
50 m_macPopUpMenuHandle
= NewMenu( 1 , "\pPopUp Menu" ) ;
51 SetControlData( m_macControl
, kControlNoPart
, kControlPopupButtonMenuHandleTag
, sizeof( MenuHandle
) , (char*) &m_macPopUpMenuHandle
) ;
52 SetControlMinimum( m_macControl
, 0 ) ;
53 SetControlMaximum( m_macControl
, 0) ;
55 SetControlValue( m_macControl
, 1 ) ;
57 MacPostControlCreate() ;
59 for ( int i
= 0; i
< n
; i
++ )
66 // ----------------------------------------------------------------------------
67 // adding/deleting items to/from the list
68 // ----------------------------------------------------------------------------
70 int wxChoice::DoAppend(const wxString
& item
)
73 wxMenuItem::MacBuildMenuString( label
, NULL
, NULL
, item
,false);
74 AppendMenu( m_macPopUpMenuHandle
, label
) ;
75 m_strings
.Add( item
) ;
77 int index
= m_strings
.GetCount() - 1 ;
78 DoSetItemClientData( index
, NULL
) ;
79 SetControlMaximum( m_macControl
, Number()) ;
83 void wxChoice::Delete(int n
)
85 wxCHECK_RET( n
< GetCount(), wxT("invalid item index in wxChoice::Delete") );
87 if ( HasClientObjectData() )
89 delete GetClientObject(n
);
92 ::DeleteMenuItem( m_macPopUpMenuHandle
, n
+ 1) ;
93 m_strings
.Remove( n
) ;
95 SetControlMaximum( m_macControl
, Number()) ;
98 void wxChoice::Clear()
102 for ( int i
= 0 ; i
< GetCount() ; i
++ )
104 ::DeleteMenuItem( m_macPopUpMenuHandle
, 1 ) ;
108 SetControlMaximum( m_macControl
, 0 ) ;
111 void wxChoice::Free()
113 if ( HasClientObjectData() )
115 size_t count
= GetCount();
116 for ( size_t n
= 0; n
< count
; n
++ )
118 delete GetClientObject(n
);
123 // ----------------------------------------------------------------------------
125 // ----------------------------------------------------------------------------
127 int wxChoice::GetSelection() const
129 return GetControlValue( m_macControl
) -1 ;
132 void wxChoice::SetSelection(int n
)
134 SetControlValue( m_macControl
, n
+ 1 ) ;
137 // ----------------------------------------------------------------------------
138 // string list functions
139 // ----------------------------------------------------------------------------
141 int wxChoice::GetCount() const
143 return m_strings
.GetCount() ;
146 int wxChoice::FindString(const wxString
& s
) const
148 for( int i
= 0 ; i
< GetCount() ; i
++ )
150 if ( GetString( i
).IsSameAs(s
, FALSE
) )
156 void wxChoice::SetString(int n
, const wxString
& s
)
158 wxFAIL_MSG(wxT("not implemented"));
160 #if 0 // should do this, but no Insert() so far
167 wxString
wxChoice::GetString(int n
) const
169 return m_strings
[n
] ;
172 // ----------------------------------------------------------------------------
174 // ----------------------------------------------------------------------------
176 void wxChoice::DoSetItemClientData( int n
, void* clientData
)
178 wxCHECK_RET( n
>= 0 && n
< m_datas
.GetCount(),
179 "invalid index in wxChoice::SetClientData" );
180 wxASSERT_MSG( m_datas
.GetCount() >= n
, "invalid client_data array" ) ;
182 if ( m_datas
.GetCount() > n
)
184 m_datas
[n
] = (char*) clientData
;
188 m_datas
.Add( (char*) clientData
) ;
192 void *wxChoice::DoGetItemClientData(int N
) const
194 wxCHECK_MSG( N
>= 0 && N
< m_datas
.GetCount(), NULL
,
195 "invalid index in wxChoice::GetClientData" );
197 return (void *)m_datas
[N
];
200 void wxChoice::DoSetItemClientObject( int n
, wxClientData
* clientData
)
202 DoSetItemClientData(n
, clientData
);
205 wxClientData
* wxChoice::DoGetItemClientObject( int n
) const
207 return (wxClientData
*)DoGetItemClientData(n
);
210 void wxChoice::MacHandleControlClick( ControlHandle control
, SInt16 controlpart
)
212 wxCommandEvent
event(wxEVT_COMMAND_CHOICE_SELECTED
, m_windowId
);
213 event
.SetInt(GetSelection());
214 event
.SetEventObject(this);
215 event
.SetString(GetStringSelection());
216 ProcessCommand(event
);
219 void wxChoice::Command(wxCommandEvent & event)
221 SetSelection (event.GetInt());
222 ProcessCommand (event);