]>
Commit | Line | Data |
---|---|---|
2ac013b1 | 1 | ///////////////////////////////////////////////////////////////////////////// |
e9576ca5 SC |
2 | // Name: choice.cpp |
3 | // Purpose: wxChoice | |
a31a5f85 | 4 | // Author: Stefan Csomor |
e9576ca5 | 5 | // Modified by: |
a31a5f85 | 6 | // Created: 1998-01-01 |
e9576ca5 | 7 | // RCS-ID: $Id$ |
a31a5f85 | 8 | // Copyright: (c) Stefan Csomor |
65571936 | 9 | // Licence: wxWindows licence |
e9576ca5 | 10 | ///////////////////////////////////////////////////////////////////////////// |
e40298d5 | 11 | |
a8e9860d | 12 | #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) |
e9576ca5 SC |
13 | #pragma implementation "choice.h" |
14 | #endif | |
e40298d5 | 15 | |
a8e9860d | 16 | #include "wx/wxprec.h" |
312ebad4 WS |
17 | |
18 | #if wxUSE_CHOICE | |
19 | ||
e9576ca5 | 20 | #include "wx/choice.h" |
03e11df5 | 21 | #include "wx/menu.h" |
519cb848 | 22 | #include "wx/mac/uma.h" |
e40298d5 | 23 | |
2f1ae414 | 24 | #if !USE_SHARED_LIBRARY |
e9576ca5 | 25 | IMPLEMENT_DYNAMIC_CLASS(wxChoice, wxControl) |
2f1ae414 | 26 | #endif |
e40298d5 | 27 | |
892e461e | 28 | extern MenuHandle NewUniqueMenu() ; |
e40298d5 | 29 | |
5b781a67 SC |
30 | wxChoice::~wxChoice() |
31 | { | |
d76240bf MB |
32 | if ( HasClientObjectData() ) |
33 | { | |
34 | size_t i, max = GetCount(); | |
35 | ||
36 | for ( i = 0; i < max; ++i ) | |
37 | delete GetClientObject(i); | |
38 | } | |
39 | ||
40 | // DeleteMenu( m_macPopUpMenuId ) ; | |
e40298d5 | 41 | // DisposeMenu( m_macPopUpMenuHandle ) ; |
5b781a67 | 42 | } |
e40298d5 | 43 | |
584ad2a3 MB |
44 | bool wxChoice::Create(wxWindow *parent, wxWindowID id, |
45 | const wxPoint& pos, | |
46 | const wxSize& size, | |
47 | const wxArrayString& choices, | |
48 | long style, | |
49 | const wxValidator& validator, | |
50 | const wxString& name) | |
51 | { | |
52 | wxCArrayString chs(choices); | |
53 | ||
54 | return Create(parent, id, pos, size, chs.GetCount(), chs.GetStrings(), | |
55 | style, validator, name); | |
56 | } | |
57 | ||
e9576ca5 SC |
58 | bool wxChoice::Create(wxWindow *parent, wxWindowID id, |
59 | const wxPoint& pos, | |
60 | const wxSize& size, | |
e40298d5 JS |
61 | int n, const wxString choices[], |
62 | long style, | |
e9576ca5 SC |
63 | const wxValidator& validator, |
64 | const wxString& name) | |
65 | { | |
312ebad4 WS |
66 | m_macIsUserPane = false ; |
67 | ||
b45ed7a2 VZ |
68 | if ( !wxChoiceBase::Create(parent, id, pos, size, style, validator, name) ) |
69 | return false; | |
70 | ||
facd6764 | 71 | Rect bounds = wxMacGetBoundsForControl( this , pos , size ) ; |
4c37f124 | 72 | |
b905d6cc | 73 | m_peer = new wxMacControl(this) ; |
312ebad4 | 74 | verify_noerr ( CreatePopupButtonControl( MAC_WXHWND(parent->MacGetTopLevelWindowRef()) , &bounds , CFSTR("") , |
5ca0d812 | 75 | -12345 , false /* no variable width */ , 0 , 0 , 0 , m_peer->GetControlRefAddr() ) ); |
312ebad4 | 76 | |
c69279ef | 77 | |
e40298d5 | 78 | m_macPopUpMenuHandle = NewUniqueMenu() ; |
21fd5529 SC |
79 | m_peer->SetData<MenuHandle>( kControlNoPart , kControlPopupButtonMenuHandleTag , (MenuHandle) m_macPopUpMenuHandle ) ; |
80 | m_peer->SetValueAndRange( n > 0 ? 1 : 0 , 0 , 0 ) ; | |
facd6764 | 81 | MacPostControlCreate(pos,size) ; |
34585919 | 82 | |
7b6986c3 JS |
83 | // FIXME: STL version of wxArrayString doesn't have the same args |
84 | #if !wxUSE_STL | |
34585919 SC |
85 | if ( style & wxCB_SORT ) |
86 | { | |
87 | m_strings = wxArrayString(1) ; // autosort | |
88 | } | |
7b6986c3 | 89 | #endif |
34585919 | 90 | |
b668a735 SC |
91 | for ( int i = 0; i < n; i++ ) |
92 | { | |
93 | Append(choices[i]); | |
94 | } | |
d3b5db4b | 95 | SetBestSize(size); // Needed because it is a wxControlWithItems |
312ebad4 | 96 | return true; |
e9576ca5 | 97 | } |
e40298d5 | 98 | |
b668a735 SC |
99 | // ---------------------------------------------------------------------------- |
100 | // adding/deleting items to/from the list | |
101 | // ---------------------------------------------------------------------------- | |
2597135a | 102 | int wxChoice::DoAppend(const wxString& item) |
e9576ca5 | 103 | { |
34585919 SC |
104 | size_t index = m_strings.Add( item ) ; |
105 | m_datas.Insert( NULL , index ) ; | |
106 | UMAInsertMenuItem(MAC_WXHMENU( m_macPopUpMenuHandle ) , item, m_font.GetEncoding() , index ); | |
e40298d5 | 107 | DoSetItemClientData( index , NULL ) ; |
21fd5529 | 108 | m_peer->SetMaximum( GetCount() ) ; |
e40298d5 | 109 | return index ; |
2597135a | 110 | } |
e40298d5 | 111 | |
243dbf1a VZ |
112 | int wxChoice::DoInsert(const wxString& item, int pos) |
113 | { | |
114 | wxCHECK_MSG(!(GetWindowStyle() & wxCB_SORT), -1, wxT("can't insert into sorted list")); | |
115 | wxCHECK_MSG((pos>=0) && (pos<=GetCount()), -1, wxT("invalid index")); | |
116 | ||
117 | if (pos == GetCount()) | |
118 | return DoAppend(item); | |
119 | ||
34585919 | 120 | UMAInsertMenuItem(MAC_WXHMENU( m_macPopUpMenuHandle ) , item, m_font.GetEncoding() , pos ); |
243dbf1a VZ |
121 | m_strings.Insert( item, pos ) ; |
122 | m_datas.Insert( NULL, pos ) ; | |
123 | DoSetItemClientData( pos , NULL ) ; | |
21fd5529 | 124 | m_peer->SetMaximum( GetCount() ) ; |
243dbf1a VZ |
125 | return pos ; |
126 | } | |
127 | ||
b668a735 | 128 | void wxChoice::Delete(int n) |
2597135a | 129 | { |
b668a735 | 130 | wxCHECK_RET( n < GetCount(), wxT("invalid item index in wxChoice::Delete") ); |
b668a735 | 131 | if ( HasClientObjectData() ) |
2597135a | 132 | { |
b668a735 | 133 | delete GetClientObject(n); |
2597135a | 134 | } |
76a5e5d2 | 135 | ::DeleteMenuItem( MAC_WXHMENU(m_macPopUpMenuHandle) , n + 1) ; |
5fe38474 | 136 | m_strings.RemoveAt( n ) ; |
3ef585df | 137 | m_datas.RemoveAt( n ) ; |
21fd5529 | 138 | m_peer->SetMaximum( GetCount() ) ; |
e9576ca5 | 139 | } |
e40298d5 | 140 | |
e9576ca5 SC |
141 | void wxChoice::Clear() |
142 | { | |
4b651a46 | 143 | FreeData(); |
b668a735 | 144 | for ( int i = 0 ; i < GetCount() ; i++ ) |
519cb848 | 145 | { |
e40298d5 | 146 | ::DeleteMenuItem( MAC_WXHMENU(m_macPopUpMenuHandle) , 1 ) ; |
4b651a46 | 147 | } |
b668a735 SC |
148 | m_strings.Empty() ; |
149 | m_datas.Empty() ; | |
21fd5529 | 150 | m_peer->SetMaximum( 0 ) ; |
e9576ca5 | 151 | } |
e40298d5 | 152 | |
4b651a46 | 153 | void wxChoice::FreeData() |
b668a735 SC |
154 | { |
155 | if ( HasClientObjectData() ) | |
156 | { | |
157 | size_t count = GetCount(); | |
158 | for ( size_t n = 0; n < count; n++ ) | |
159 | { | |
160 | delete GetClientObject(n); | |
161 | } | |
162 | } | |
163 | } | |
e40298d5 | 164 | |
b668a735 SC |
165 | // ---------------------------------------------------------------------------- |
166 | // selection | |
167 | // ---------------------------------------------------------------------------- | |
e9576ca5 SC |
168 | int wxChoice::GetSelection() const |
169 | { | |
21fd5529 | 170 | return m_peer->GetValue() -1 ; |
e9576ca5 | 171 | } |
e40298d5 | 172 | |
b668a735 | 173 | void wxChoice::SetSelection(int n) |
519cb848 | 174 | { |
21fd5529 | 175 | m_peer->SetValue( n + 1 ) ; |
519cb848 | 176 | } |
e40298d5 | 177 | |
b668a735 SC |
178 | // ---------------------------------------------------------------------------- |
179 | // string list functions | |
180 | // ---------------------------------------------------------------------------- | |
e40298d5 | 181 | |
b668a735 | 182 | int wxChoice::GetCount() const |
e9576ca5 | 183 | { |
b668a735 | 184 | return m_strings.GetCount() ; |
e9576ca5 | 185 | } |
e40298d5 | 186 | |
e9576ca5 SC |
187 | int wxChoice::FindString(const wxString& s) const |
188 | { | |
34585919 | 189 | return m_strings.Index( s , true , false) ; |
b668a735 | 190 | } |
e40298d5 | 191 | |
b668a735 SC |
192 | void wxChoice::SetString(int n, const wxString& s) |
193 | { | |
34585919 SC |
194 | m_strings[n] = s ; |
195 | // apple menu pos is 1-based | |
196 | UMASetMenuItemText( MAC_WXHMENU(m_macPopUpMenuHandle) , n + 1 , s , wxFont::GetDefaultEncoding() ) ; | |
e9576ca5 SC |
197 | } |
198 | ||
199 | wxString wxChoice::GetString(int n) const | |
200 | { | |
17a6a662 VZ |
201 | wxCHECK_MSG( n >= 0 && (size_t)n < m_strings.GetCount(), _T(""), |
202 | _T("wxChoice::GetString(): invalid index") ); | |
203 | ||
e40298d5 | 204 | return m_strings[n] ; |
e9576ca5 | 205 | } |
17a6a662 | 206 | |
b668a735 SC |
207 | // ---------------------------------------------------------------------------- |
208 | // client data | |
209 | // ---------------------------------------------------------------------------- | |
b668a735 SC |
210 | void wxChoice::DoSetItemClientData( int n, void* clientData ) |
211 | { | |
d81cc883 | 212 | wxCHECK_RET( n >= 0 && (size_t)n < m_datas.GetCount(), |
427ff662 | 213 | wxT("invalid index in wxChoice::SetClientData") ); |
c69279ef | 214 | |
d81cc883 | 215 | m_datas[n] = (char*) clientData ; |
b668a735 | 216 | } |
e40298d5 | 217 | |
d81cc883 | 218 | void *wxChoice::DoGetItemClientData(int n) const |
b668a735 | 219 | { |
3ba18664 RD |
220 | wxCHECK_MSG( n >= 0 && (size_t)n < m_datas.GetCount(), NULL, |
221 | wxT("invalid index in wxChoice::GetClientData") ); | |
d81cc883 | 222 | return (void *)m_datas[n]; |
b668a735 | 223 | } |
e40298d5 | 224 | |
b668a735 SC |
225 | void wxChoice::DoSetItemClientObject( int n, wxClientData* clientData ) |
226 | { | |
227 | DoSetItemClientData(n, clientData); | |
228 | } | |
e40298d5 | 229 | |
b668a735 | 230 | wxClientData* wxChoice::DoGetItemClientObject( int n ) const |
e9576ca5 | 231 | { |
b668a735 | 232 | return (wxClientData *)DoGetItemClientData(n); |
e9576ca5 | 233 | } |
e40298d5 | 234 | |
312ebad4 | 235 | wxInt32 wxChoice::MacControlHit(WXEVENTHANDLERREF WXUNUSED(handler) , WXEVENTREF WXUNUSED(event) ) |
b668a735 SC |
236 | { |
237 | wxCommandEvent event(wxEVT_COMMAND_CHOICE_SELECTED, m_windowId ); | |
f2d990ad SC |
238 | int n = GetSelection(); |
239 | // actually n should be made sure by the os to be a valid selection, but ... | |
240 | if ( n > -1 ) | |
241 | { | |
242 | event.SetInt( n ); | |
243 | event.SetString(GetStringSelection()); | |
244 | event.SetEventObject(this); | |
f2d990ad SC |
245 | if ( HasClientObjectData() ) |
246 | event.SetClientObject( GetClientObject(n) ); | |
247 | else if ( HasClientUntypedData() ) | |
248 | event.SetClientData( GetClientData(n) ); | |
f2d990ad SC |
249 | ProcessCommand(event); |
250 | } | |
4c37f124 | 251 | return noErr ; |
9453cf2b | 252 | } |
e40298d5 | 253 | |
f2d990ad | 254 | wxSize wxChoice::DoGetBestSize() const |
b668a735 | 255 | { |
637988a4 | 256 | int lbWidth = GetCount() > 0 ? 20 : 100; // some defaults |
f2d990ad SC |
257 | int lbHeight = 20; |
258 | int wLine; | |
259 | #if TARGET_CARBON | |
260 | long metric ; | |
c69279ef RD |
261 | GetThemeMetric(kThemeMetricPopupButtonHeight , &metric ); |
262 | lbHeight = metric ; | |
f2d990ad | 263 | #endif |
e40298d5 | 264 | { |
facd6764 | 265 | wxMacPortStateHelper st( UMAGetWindowPort( (WindowRef) MacGetTopLevelWindowRef() ) ) ; |
fcb35beb | 266 | if ( m_font.Ok() ) |
e40298d5 | 267 | { |
facd6764 SC |
268 | ::TextFont( m_font.MacGetFontNum() ) ; |
269 | ::TextSize( m_font.MacGetFontSize() ) ; | |
270 | ::TextFace( m_font.MacGetFontStyle() ) ; | |
e40298d5 JS |
271 | } |
272 | else | |
273 | { | |
274 | ::TextFont( kFontIDMonaco ) ; | |
275 | ::TextSize( 9 ); | |
276 | ::TextFace( 0 ) ; | |
277 | } | |
278 | // Find the widest line | |
279 | for(int i = 0; i < GetCount(); i++) { | |
280 | wxString str(GetString(i)); | |
2c1a3312 SC |
281 | #if wxUSE_UNICODE |
282 | Point bounds={0,0} ; | |
283 | SInt16 baseline ; | |
a9412f8f | 284 | ::GetThemeTextDimensions( wxMacCFStringHolder( str , m_font.GetEncoding() ) , |
2c1a3312 SC |
285 | kThemeCurrentPortFont, |
286 | kThemeStateActive, | |
287 | false, | |
288 | &bounds, | |
289 | &baseline ); | |
290 | wLine = bounds.h ; | |
291 | #else | |
939fba6c | 292 | wLine = ::TextWidth( str.c_str() , 0 , str.Length() ) ; |
2c1a3312 | 293 | #endif |
e40298d5 JS |
294 | lbWidth = wxMax(lbWidth, wLine); |
295 | } | |
296 | // Add room for the popup arrow | |
297 | lbWidth += 2 * lbHeight ; | |
298 | // And just a bit more | |
e40298d5 JS |
299 | int cx = ::TextWidth( "X" , 0 , 1 ) ; |
300 | lbWidth += cx ; | |
c69279ef | 301 | |
e40298d5 | 302 | } |
f2d990ad | 303 | return wxSize(lbWidth, lbHeight); |
d76240bf | 304 | } |
312ebad4 WS |
305 | |
306 | #endif // wxUSE_CHOICE |