]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/choice.cpp
copied wxMac and wxMotif faq from wxWebSite
[wxWidgets.git] / src / mac / carbon / choice.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: choice.cpp
3 // Purpose: wxChoice
4 // Author: AUTHOR
5 // Modified by:
6 // Created: ??/??/98
7 // RCS-ID: $Id$
8 // Copyright: (c) AUTHOR
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation "choice.h"
14 #endif
15
16 #include "wx/defs.h"
17
18 #include "wx/choice.h"
19 #include "wx/menu.h"
20 #include "wx/mac/uma.h"
21
22 #if !USE_SHARED_LIBRARY
23 IMPLEMENT_DYNAMIC_CLASS(wxChoice, wxControl)
24 #endif
25
26 short nextMenuId = 100 ; // wxMenu takes the lower ids
27
28 wxChoice::~wxChoice()
29 {
30 // DeleteMenu( m_macPopUpMenuId ) ;
31 // DisposeMenu( m_macPopUpMenuHandle ) ;
32 }
33
34 bool wxChoice::Create(wxWindow *parent, wxWindowID id,
35 const wxPoint& pos,
36 const wxSize& size,
37 int n, const wxString choices[],
38 long style,
39 const wxValidator& validator,
40 const wxString& name)
41 {
42
43 Rect bounds ;
44 Str255 title ;
45
46 MacPreControlCreate( parent , id , "" , pos , size ,style, validator , name , &bounds , title ) ;
47
48 m_macControl = UMANewControl( parent->GetMacRootWindow() , &bounds , title , false , 0 , -12345 , 0 ,
49 kControlPopupButtonProc + kControlPopupFixedWidthVariant , (long) this ) ;
50
51 m_macPopUpMenuHandle = NewMenu( 1 , "\pPopUp Menu" ) ;
52 SetControlData( m_macControl , kControlNoPart , kControlPopupButtonMenuHandleTag , sizeof( MenuHandle ) , (char*) &m_macPopUpMenuHandle) ;
53 SetControlMinimum( m_macControl , 0 ) ;
54 SetControlMaximum( m_macControl , 0) ;
55 if ( n > 0 )
56 SetControlValue( m_macControl , 1 ) ;
57
58 MacPostControlCreate() ;
59
60 for ( int i = 0; i < n; i++ )
61 {
62 Append(choices[i]);
63 }
64 return TRUE;
65 }
66
67 // ----------------------------------------------------------------------------
68 // adding/deleting items to/from the list
69 // ----------------------------------------------------------------------------
70
71 int wxChoice::DoAppend(const wxString& item)
72 {
73 Str255 label;
74 wxMenuItem::MacBuildMenuString( label , NULL , NULL , item ,false);
75 AppendMenu( m_macPopUpMenuHandle , label ) ;
76 m_strings.Add( item ) ;
77 m_datas.Add( NULL ) ;
78 int index = m_strings.GetCount() - 1 ;
79 DoSetItemClientData( index , NULL ) ;
80 SetControlMaximum( m_macControl , GetCount()) ;
81 return index ;
82 }
83
84 void wxChoice::Delete(int n)
85 {
86 wxCHECK_RET( n < GetCount(), wxT("invalid item index in wxChoice::Delete") );
87
88 if ( HasClientObjectData() )
89 {
90 delete GetClientObject(n);
91 }
92
93 ::DeleteMenuItem( m_macPopUpMenuHandle , n + 1) ;
94 m_strings.Remove( n ) ;
95 m_datas.Remove( n ) ;
96 SetControlMaximum( m_macControl , GetCount()) ;
97 }
98
99 void wxChoice::Clear()
100 {
101 Free();
102
103 for ( int i = 0 ; i < GetCount() ; i++ )
104 {
105 ::DeleteMenuItem( m_macPopUpMenuHandle , 1 ) ;
106 }
107 m_strings.Empty() ;
108 m_datas.Empty() ;
109 SetControlMaximum( m_macControl , 0 ) ;
110 }
111
112 void wxChoice::Free()
113 {
114 if ( HasClientObjectData() )
115 {
116 size_t count = GetCount();
117 for ( size_t n = 0; n < count; n++ )
118 {
119 delete GetClientObject(n);
120 }
121 }
122 }
123
124 // ----------------------------------------------------------------------------
125 // selection
126 // ----------------------------------------------------------------------------
127
128 int wxChoice::GetSelection() const
129 {
130 return GetControlValue( m_macControl ) -1 ;
131 }
132
133 void wxChoice::SetSelection(int n)
134 {
135 SetControlValue( m_macControl , n + 1 ) ;
136 }
137
138 // ----------------------------------------------------------------------------
139 // string list functions
140 // ----------------------------------------------------------------------------
141
142 int wxChoice::GetCount() const
143 {
144 return m_strings.GetCount() ;
145 }
146
147 int wxChoice::FindString(const wxString& s) const
148 {
149 for( int i = 0 ; i < GetCount() ; i++ )
150 {
151 if ( GetString( i ).IsSameAs(s, FALSE) )
152 return i ;
153 }
154 return wxNOT_FOUND ;
155 }
156
157 void wxChoice::SetString(int n, const wxString& s)
158 {
159 wxFAIL_MSG(wxT("not implemented"));
160
161 #if 0 // should do this, but no Insert() so far
162 Delete(n);
163 Insert(n + 1, s);
164 #endif
165 }
166
167
168 wxString wxChoice::GetString(int n) const
169 {
170 return m_strings[n] ;
171 }
172
173 // ----------------------------------------------------------------------------
174 // client data
175 // ----------------------------------------------------------------------------
176
177 void wxChoice::DoSetItemClientData( int n, void* clientData )
178 {
179 wxCHECK_RET( n >= 0 && n < m_datas.GetCount(),
180 "invalid index in wxChoice::SetClientData" );
181 wxASSERT_MSG( m_datas.GetCount() >= n , "invalid client_data array" ) ;
182
183 if ( m_datas.GetCount() > n )
184 {
185 m_datas[n] = (char*) clientData ;
186 }
187 else
188 {
189 m_datas.Add( (char*) clientData ) ;
190 }
191 }
192
193 void *wxChoice::DoGetItemClientData(int N) const
194 {
195 wxCHECK_MSG( N >= 0 && N < m_datas.GetCount(), NULL,
196 "invalid index in wxChoice::GetClientData" );
197
198 return (void *)m_datas[N];
199 }
200
201 void wxChoice::DoSetItemClientObject( int n, wxClientData* clientData )
202 {
203 DoSetItemClientData(n, clientData);
204 }
205
206 wxClientData* wxChoice::DoGetItemClientObject( int n ) const
207 {
208 return (wxClientData *)DoGetItemClientData(n);
209 }
210
211 void wxChoice::MacHandleControlClick( ControlHandle control , SInt16 controlpart )
212 {
213 wxCommandEvent event(wxEVT_COMMAND_CHOICE_SELECTED, m_windowId );
214 event.SetInt(GetSelection());
215 event.SetEventObject(this);
216 event.SetString(GetStringSelection());
217 ProcessCommand(event);
218 }
219 /*
220 void wxChoice::Command(wxCommandEvent & event)
221 {
222 SetSelection (event.GetInt());
223 ProcessCommand (event);
224 }
225 */