]>
Commit | Line | Data |
---|---|---|
e9576ca5 SC |
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 | ||
d8c736e5 GD |
16 | #include "wx/defs.h" |
17 | ||
e9576ca5 | 18 | #include "wx/choice.h" |
03e11df5 | 19 | #include "wx/menu.h" |
519cb848 | 20 | #include "wx/mac/uma.h" |
e9576ca5 | 21 | |
2f1ae414 | 22 | #if !USE_SHARED_LIBRARY |
e9576ca5 | 23 | IMPLEMENT_DYNAMIC_CLASS(wxChoice, wxControl) |
2f1ae414 | 24 | #endif |
e9576ca5 | 25 | |
892e461e | 26 | extern MenuHandle NewUniqueMenu() ; |
5b781a67 SC |
27 | |
28 | wxChoice::~wxChoice() | |
29 | { | |
30 | // DeleteMenu( m_macPopUpMenuId ) ; | |
e7b596fb | 31 | // DisposeMenu( m_macPopUpMenuHandle ) ; |
5b781a67 SC |
32 | } |
33 | ||
e9576ca5 SC |
34 | bool wxChoice::Create(wxWindow *parent, wxWindowID id, |
35 | const wxPoint& pos, | |
36 | const wxSize& size, | |
03e11df5 GD |
37 | int n, const wxString choices[], |
38 | long style, | |
e9576ca5 SC |
39 | const wxValidator& validator, |
40 | const wxString& name) | |
41 | { | |
e9576ca5 | 42 | |
519cb848 SC |
43 | Rect bounds ; |
44 | Str255 title ; | |
45 | ||
46 | MacPreControlCreate( parent , id , "" , pos , size ,style, validator , name , &bounds , title ) ; | |
2f1ae414 | 47 | |
76a5e5d2 | 48 | m_macControl = ::NewControl( MAC_WXHWND(parent->MacGetRootWindow()) , &bounds , title , false , 0 , -12345 , 0 , |
8208e181 | 49 | kControlPopupButtonProc + kControlPopupFixedWidthVariant , (long) this ) ; |
519cb848 | 50 | |
892e461e | 51 | m_macPopUpMenuHandle = NewUniqueMenu() ; |
76a5e5d2 | 52 | SetControlData( (ControlHandle) m_macControl , kControlNoPart , kControlPopupButtonMenuHandleTag , sizeof( MenuHandle ) , (char*) &m_macPopUpMenuHandle) ; |
467e3168 SC |
53 | SetControl32BitMinimum( (ControlHandle) m_macControl , 0 ) ; |
54 | SetControl32BitMaximum( (ControlHandle) m_macControl , 0) ; | |
2f1ae414 | 55 | if ( n > 0 ) |
467e3168 | 56 | SetControl32BitValue( (ControlHandle) m_macControl , 1 ) ; |
519cb848 SC |
57 | |
58 | MacPostControlCreate() ; | |
59 | ||
b668a735 SC |
60 | for ( int i = 0; i < n; i++ ) |
61 | { | |
62 | Append(choices[i]); | |
63 | } | |
519cb848 | 64 | return TRUE; |
e9576ca5 SC |
65 | } |
66 | ||
b668a735 SC |
67 | // ---------------------------------------------------------------------------- |
68 | // adding/deleting items to/from the list | |
69 | // ---------------------------------------------------------------------------- | |
70 | ||
2597135a | 71 | int wxChoice::DoAppend(const wxString& item) |
e9576ca5 | 72 | { |
2f1ae414 SC |
73 | Str255 label; |
74 | wxMenuItem::MacBuildMenuString( label , NULL , NULL , item ,false); | |
76a5e5d2 | 75 | AppendMenu( MAC_WXHMENU( m_macPopUpMenuHandle ) , label ) ; |
2f1ae414 | 76 | m_strings.Add( item ) ; |
b668a735 SC |
77 | m_datas.Add( NULL ) ; |
78 | int index = m_strings.GetCount() - 1 ; | |
79 | DoSetItemClientData( index , NULL ) ; | |
467e3168 | 80 | SetControl32BitMaximum( (ControlHandle) m_macControl , GetCount()) ; |
b668a735 | 81 | return index ; |
2597135a SC |
82 | } |
83 | ||
b668a735 | 84 | void wxChoice::Delete(int n) |
2597135a | 85 | { |
b668a735 | 86 | wxCHECK_RET( n < GetCount(), wxT("invalid item index in wxChoice::Delete") ); |
e9576ca5 | 87 | |
b668a735 | 88 | if ( HasClientObjectData() ) |
2597135a | 89 | { |
b668a735 | 90 | delete GetClientObject(n); |
2597135a | 91 | } |
03e11df5 | 92 | |
76a5e5d2 | 93 | ::DeleteMenuItem( MAC_WXHMENU(m_macPopUpMenuHandle) , n + 1) ; |
2f1ae414 | 94 | m_strings.Remove( n ) ; |
3ef585df | 95 | m_datas.RemoveAt( n ) ; |
467e3168 | 96 | SetControl32BitMaximum( (ControlHandle) m_macControl , GetCount()) ; |
e9576ca5 SC |
97 | } |
98 | ||
99 | void wxChoice::Clear() | |
100 | { | |
4b651a46 | 101 | FreeData(); |
b668a735 SC |
102 | |
103 | for ( int i = 0 ; i < GetCount() ; i++ ) | |
519cb848 | 104 | { |
76a5e5d2 | 105 | ::DeleteMenuItem( MAC_WXHMENU(m_macPopUpMenuHandle) , 1 ) ; |
4b651a46 | 106 | } |
b668a735 SC |
107 | m_strings.Empty() ; |
108 | m_datas.Empty() ; | |
467e3168 | 109 | SetControl32BitMaximum( (ControlHandle) m_macControl , 0 ) ; |
e9576ca5 SC |
110 | } |
111 | ||
4b651a46 | 112 | void wxChoice::FreeData() |
b668a735 SC |
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 | ||
e9576ca5 SC |
128 | int wxChoice::GetSelection() const |
129 | { | |
467e3168 | 130 | return GetControl32BitValue( (ControlHandle) m_macControl ) -1 ; |
e9576ca5 SC |
131 | } |
132 | ||
b668a735 | 133 | void wxChoice::SetSelection(int n) |
519cb848 | 134 | { |
467e3168 | 135 | SetControl32BitValue( (ControlHandle) m_macControl , n + 1 ) ; |
519cb848 SC |
136 | } |
137 | ||
b668a735 SC |
138 | // ---------------------------------------------------------------------------- |
139 | // string list functions | |
140 | // ---------------------------------------------------------------------------- | |
519cb848 | 141 | |
b668a735 | 142 | int wxChoice::GetCount() const |
e9576ca5 | 143 | { |
b668a735 | 144 | return m_strings.GetCount() ; |
e9576ca5 SC |
145 | } |
146 | ||
147 | int wxChoice::FindString(const wxString& s) const | |
148 | { | |
b668a735 | 149 | for( int i = 0 ; i < GetCount() ; i++ ) |
519cb848 | 150 | { |
b668a735 | 151 | if ( GetString( i ).IsSameAs(s, FALSE) ) |
519cb848 SC |
152 | return i ; |
153 | } | |
b668a735 SC |
154 | return wxNOT_FOUND ; |
155 | } | |
156 | ||
157 | void wxChoice::SetString(int n, const wxString& s) | |
158 | { | |
4b651a46 | 159 | wxFAIL_MSG(wxT("wxChoice::SetString() not yet implemented")); |
b668a735 SC |
160 | |
161 | #if 0 // should do this, but no Insert() so far | |
162 | Delete(n); | |
163 | Insert(n + 1, s); | |
164 | #endif | |
e9576ca5 SC |
165 | } |
166 | ||
b668a735 | 167 | |
e9576ca5 SC |
168 | wxString wxChoice::GetString(int n) const |
169 | { | |
2f1ae414 | 170 | return m_strings[n] ; |
e9576ca5 SC |
171 | } |
172 | ||
b668a735 SC |
173 | // ---------------------------------------------------------------------------- |
174 | // client data | |
175 | // ---------------------------------------------------------------------------- | |
176 | ||
177 | void wxChoice::DoSetItemClientData( int n, void* clientData ) | |
178 | { | |
d81cc883 | 179 | wxCHECK_RET( n >= 0 && (size_t)n < m_datas.GetCount(), |
b668a735 | 180 | "invalid index in wxChoice::SetClientData" ); |
b668a735 | 181 | |
d81cc883 | 182 | m_datas[n] = (char*) clientData ; |
b668a735 SC |
183 | } |
184 | ||
d81cc883 | 185 | void *wxChoice::DoGetItemClientData(int n) const |
b668a735 | 186 | { |
d81cc883 | 187 | wxCHECK_MSG( n >= 0 && (size_t)n < m_datas.GetCount(), NULL, |
b668a735 SC |
188 | "invalid index in wxChoice::GetClientData" ); |
189 | ||
d81cc883 | 190 | return (void *)m_datas[n]; |
b668a735 SC |
191 | } |
192 | ||
193 | void wxChoice::DoSetItemClientObject( int n, wxClientData* clientData ) | |
194 | { | |
195 | DoSetItemClientData(n, clientData); | |
196 | } | |
197 | ||
198 | wxClientData* wxChoice::DoGetItemClientObject( int n ) const | |
e9576ca5 | 199 | { |
b668a735 | 200 | return (wxClientData *)DoGetItemClientData(n); |
e9576ca5 SC |
201 | } |
202 | ||
76a5e5d2 | 203 | void wxChoice::MacHandleControlClick( WXWidget control , wxInt16 controlpart ) |
b668a735 SC |
204 | { |
205 | wxCommandEvent event(wxEVT_COMMAND_CHOICE_SELECTED, m_windowId ); | |
206 | event.SetInt(GetSelection()); | |
207 | event.SetEventObject(this); | |
208 | event.SetString(GetStringSelection()); | |
209 | ProcessCommand(event); | |
210 | } | |
9453cf2b SC |
211 | |
212 | wxSize wxChoice::DoGetBestSize() const | |
213 | { | |
214 | // TODO should modify this to take into account string length ala wxGTK | |
215 | return wxSize(100,20); | |
216 | } | |
217 | ||
b668a735 SC |
218 | /* |
219 | void wxChoice::Command(wxCommandEvent & event) | |
220 | { | |
221 | SetSelection (event.GetInt()); | |
222 | ProcessCommand (event); | |
223 | } | |
224 | */ |