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